tags:

views:

171

answers:

6

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

+3  A: 

If you don't need Exception's variable to get some information from it, don't declare it

try { }
catch ( ) 

is equal to

try { }
catch (Exception) { }

Use this

try { }
catch (Exception ex) { var m = ex.Message; }

if you need some information to gather.

Use this

try { }
catch (FooException) { }
catch (BarException) { }

if you need to catch only specific types of exceptions, i.e. SomeAnotherException will not be caught.

abatishchev
+2  A: 

It would be better if you just let the exception bubble all the way up and use an application wide exception handler or something like ELMAH. Usually you'll want to log the exception or something so there's a record of stuff failing.

R0MANARMY
yeah, but that is only for 'unhandled' exceptions it seems. i am handling this one. don't need reporting or anything. perhaps i can update the question with a SPECIFIC exception not any.
b0x0rz
i do like the ELMAH though so great TIP ;)
b0x0rz
A: 

I usually declare it and suffer with the warning since it can be very useful to be able to look at the exception details while debugging.

ho1
You can use Locals window during debug to check out automatically declared variable `$exception`. It's also available in Immediate window.
abatishchev
@abatishchev - Excellent, I didn't know that. Thank you very much!
ho1
@ho1: Glad it helped! :)
abatishchev
A: 

There are two reasons to declare an exception variable in a catch block. To catch only specific exception types or to do something with the exception info. In your case you are doing neither so t serves no purpose.

Ben Robinson
+5  A: 

You just don't declare the variable:

try
{
   // do something
}
catch (SpecificException)
{
    Response.Redirect("~/InformUserAboutAn/InternalException/");
}

This is a moot point when catching System.Exception (in your original example, which is not exactly the same as an empty catch -- an empty catch will 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 Exception is 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 an Exception on a file delete (or something like that), but the main logic wasn't working correctly. We should have been only catching an IOException, but we were catching and discarding the NullReferenceException that was causing the failure.

That's not to say you never should catch Exception; just rarely.

Mark Rushakoff
i see, thnx. i updated the question with a Specific Exception. that was my bad in explaining it.
b0x0rz
There is not need to declare use `catch (Exception)` if System.Exception is used. This syntax is needed when some other type is been catching, i.e. `catch (NullReferenceException)`
abatishchev
will accept in 7 minutes :P
b0x0rz
@b0x0rz: Yeah, if you need to address a specific exception, this is the way to go....still seems like an odd thing to do. This is something you could (in theory) do in the global exception handler as well. Just beware the pokemon.
R0MANARMY
`catch (Exception)` and `catch` are different.
John Saunders
@John: I was not aware of that. I changed the answer slightly and put a link to an article explaining the difference. Thanks.
Mark Rushakoff
@Jonh: And what is the different if `System.Exception` is used?
abatishchev
catch by const reference. Thus you prevent the system from copying the exception back from its special hidden place (also prevents slicing)
Martin York
+1  A: 

Any reason why you wouldn't let unhandled exceptions simply throw and use the Application Level error handling built into ASP.NET? See How to: Handle Application-Level Errors for more details.

micahtan