what is a best practice in cases such as this one:
try
{
// do something
}
catch (SpecificException ex)
{
Response.Redirect("~/InformUserAboutAn/InternalException/");
}
the warning i get is that ex is never used.
however all i need here is to inform the user, so i don’t have a need for it.
do i just do:
try
{
// do something
}
catch
{
Response.Redirect("~/InformUserAboutAn/InternalException/");
}
somehow i don’t like that, seems strange!!? any tips? best practices?
what would be the way to handle this.
thnx
You just don’t declare the variable:
This is a moot point when catching
System.Exception(in your original example, which is not exactly the same as an emptycatch— an emptycatchwill also catch COM exceptions, for instance), but this is the correct construct to use.If you run your code through other analysis engines (Gendarme, for instance), you will also be warned that catching a plain
Exceptionis poor practice because it can mask other exceptions besides what you really wanted to catch. That’s bitten me a few times while maintaining legacy code — we were catching and ignoring anExceptionon a file delete (or something like that), but the main logic wasn’t working correctly. We should have been only catching anIOException, but we were catching and discarding theNullReferenceExceptionthat was causing the failure.That’s not to say you never should catch
Exception; just rarely.