Examine an Exception in a Catch() Block

Found a useful nugget in Richter’s recent CLR via C# book I want to share with you. But first some background.

Sometimes when I write a catch block, I don’t really have any plans for the caught exception. The following is a contrived example that is somewhat realistic.

try
{
    DoSomething();
}
catch(System.SomeException)
{
    DoSomethingElse();
    throw;
}

In the above code, I only want DoSomethingElse() to execute if DoSomething() throws an exception of type SomeException. I can’t put DoSomethingElse() in a finally block because then it would always get called and not just when the exception is thrown. I don’t need to do anything with SomeException because I am propagating it up the callstack via the throw keyword to let some other method handle it.

But now, as I am stepping through the code in the debugger, I may actually want to examine SomeException when the debugger reaches the line DoSomethingElse(). Typically I would have to rewrite the code like so:

try
{
    DoSomething();
}
catch(System.FormatException e)
{
    DoSomethingElse();
    throw;
}

Just so I can examine the exception now stored in the variable e. This is plain dumb and Richter points out why in a little tip in his book. You can use the debugger variable $exception provided by the Visual Studio.NET Debugger to examine the exception in a catch block. I wish I had known about this a while ago.

What others have said

Requesting Gravatar... Cullen Waters Jun 07, 2006 4:19 PM
# re: Examine an Exception in a Catch() Block
Another benefit of using $exception is that putting in the System.FormatException e, and then not using the e variable results in a compiler warning.

Good tip, thanks.
Requesting Gravatar... Luke Jun 07, 2006 5:39 PM
# re: Examine an Exception in a Catch() Block
Cool, thanks for the tip :)

I'm mainly a Java programmer, and I habitually use e.printStackTrace() in all my catch blocks. Sometimes this helps me to see what is going on without even launching debugger.

Once the code is tested and ready for production, I do a quick search and replace to comment out all the printStackTrace calls.

Requesting Gravatar... Bill Pierce Jun 08, 2006 7:36 AM
# re: Examine an Exception in a Catch() Block
Phil,
Great blog (regular reader). Oren just posted something you might find useful, might be 2.0 only. Would allow you to easily DoSomethingElse() whenver an exception is thrown, similar to the way the 'using' statement will call Dispose() for you.

http://www.ayende.com/Blog/2006/06/07/CreatingYourOwnUsingStatement.aspx

-Bill
Requesting Gravatar... drew Jun 12, 2006 5:20 AM
# re: Examine an Exception in a Catch() Block
not sure I get it.. I tried your

catch(System.Exception) {
String foo = ""; //[1]
}

and at the [2] click SHift+F9 and paste "$exception" in the quick watch. It says
"identifier '$exception' out of scope".

I'm using VS2002 - could it matter?
Requesting Gravatar... Steve Crane Jun 21, 2006 9:11 AM
# re: Examine an Exception in a Catch() Block
Thanks for this. I have been wondering how to suppress the warnings about unreferenced variables, never thinking that the C# Reference and all the books I've looked at would neglect to mention that the variable name in a specific exception catch clause is optional. It takes a visit to the language specification document to find this out.
Requesting Gravatar... James Gregory Jun 22, 2006 8:09 AM
# re: Examine an Exception in a Catch() Block
I'd usually end up adding the variable name temporarily, checking the exception with the debugger, then reverting the code.

This way is so much better! Thanks.

What do you have to say?

(will show your gravatar)
Please add 8 and 4 and type the answer here: