Try the catch block to display an error message

I doubt the blocks Try Catch. If I get an error in a block Try, it will redirect the block Catchin accordance with the rule. So that I can set visible = true to my shortcut in the catch block .

ie lblError.visible=true;

Is this correct according to coding standards? I am new to development.

+3
source share
5 answers

You can set the visibility of your input elements to false in the catch block and display an error bar, for example, instead.

+2
source

, ? , lblError catch catch, try..catch , , catch.

catch , .

CustomErrors web.config , .

try
{
  //statements;
}
catch (Exception ex)
{
  ShowError(ex);
}

void ShowError(Exception ex)
{
   //Log or Email error first
   LogOrEmailError(ex);

   // you can write user friendly message based on the exception provided or a generic error message.
   lblError.Visible = true;
   lblError.Text = GetUserFriendlyErrorMessage(ex); // or throw; if you are planing to handle error in global.ascx or through CustomErrors in web.config
}
0

, . catch.

0

, , try.

, , , try .

0
source

Yes, you can write any code inside a catch block, even returnfrom a method. At one point, you should use the block finaly{}to release any resource that you use in the method in which the exception was thrown (for example, you are using a database connection).

0
source

All Articles