You're hiding your error by doing this
catch (Exception ex)
{
}
Do not do this . Hiding the exceptions never is a good idea. Probably just getting this block catch
will already help you a lot.
It is very common to think that "% -required programming" is a good option, but in fact exceptions should only be captured in specific cases when you know what (and how ) do after capturing them.
Another important thing, this code
BEGIN CATCH
Raiserror('Erro ao gerar os dados', 18, 1);
return;
END CATCH;
also does nothing very useful. You are hiding the real error and putting a "beautiful message". This is not ideal.
Imagine that this try-catch
gives an error in the production environment, the error that the application will receive will be procedure/function
, when in fact it should be informing you where and why the error occurred.
About your comment
in case of Raiserror error ('Error generating data', 18, 1); ... this will be shown to the user when he is to generate the data of a demo in the TextBox. I do not want to explode a msg error that the guy will not even know what it is.
Exceptions do not serve to inform something to the end user. They serve to show the developer that something is wrong. First it makes no difference whether or not the user knows what the error message is saying, it will remain an error message and it will not be able to do anything without the help of a support or the developer. Another important point is that if some exception is being thrown it is because there is some error in the code and the first thing you will want to do when you encounter an error is to fix it,
I can understand your intention not to scare the user with giant, code-in error messages in between. It's best if you capture the generic exceptions in the "top" layer of your application, something like the main screen, entry point, or something similar.
Take a look at Best way to handle Exceptions . There you have good answers on this subject.