DevSource Article on Exceptions

I am now a published DevSource article author. :)

Well actually, Bob Reselman (aka Mr. Coding Slave) did nearly all of the writing. I merely provided technical editing and proofing along with clarifying some sections. In return, he graciously gave me a byline.

The article is a beginners guide to exceptions.NET. and Bob did a bang-up job especially given the circumstances. He writes in a very approachable manner.

Hopefully, we can follow-up with a more in-depth version to take beginners to the next level in understanding best practices. One thing I wish we had discussed in the article is the guidelines around re-throwing exceptions. For example, I’ve seen many beginning developers make the following mistake...

public void SomeMethod()

{

    try

    {

        SomeOtherMethod(null);

    }

    catch(ArgumentNullException e)

    {

        //Code here to do something

 

        throw e; //Bad!

    }

The problem with this approach is the code in the catch clause is throwing the exception it caught. The runtime generates a stack trace from the point at which an exception is thrown. In this case, the stack trace will show the stack starting from the line throw e and not the line of code where the exception actually occurred.

If this is confusing, consider that the runtime doesn’t exactly distinguish between these three cases...

Exception exc = new Exception();

throw exc;


throw new Exception();


throw SomeExceptionBuilderFunction();

If you really intend to rethrow an exception without wrapping it in another exception, then the proper syntax is to use the throw keyword without specifying an exception. The original exception will propagate up with its stack trace intact.

public void SomeMethod()

{

    try

    {

        SomeOtherMethod(null);

    }

    catch(ArgumentNullException e)

    {

        //Code here to do something

 

        throw; //Better!

    }

However, even this can be improved on depending on why we are catching the exception in the first place. If we are performing cleanup code in the catch clause, it would be better to move that code to the finally block and not even catch the exception in the first place.

Also, using the throw syntax as illustrated above can affect the debuggability of a system. Christopher Blumme points this out in his annotation in the book Framework Design Guidelines (highly recommended) where he notes that attaching a debugger works by detecting exceptions that are left unhandled. If there is a series of catch and throw segments up the stack, the debugger might only attach to the last segment, far away from the actual point at which the exception occurred.

public void SomeMethod()

{

    try

    {

        SomeOtherMethod(null);

    }

    finally

    {

        CleanUp(); //Possibly even better

    }

}

Bob and I plan to follow-up with hopefully more articles covering exceptions. This is just a start.

What others have said

Requesting Gravatar... Emrysamity Nov 17, 2005 4:33 PM
# Losing the Stack on C# Exceptions
throw;, instead. Wow. Embarrassingly obvious. Thanks go to Mr. Haack's post for today's education.
Requesting Gravatar... Jack's WebLog Nov 17, 2005 9:34 PM
# Beginners Guide to Exceptions in .NET
Phil
Haack posted a link to an article he helped
write about handling exceptions in .NET.
Requesting Gravatar... tod Dec 06, 2005 1:01 PM
# re: DevSource Article on Exceptions
An interesting article Phil, nice job. Just an fyi...your link to the article goes to the 4th page in the article instead of the first. I read about half of the article before I realized it. :)
Requesting Gravatar... Haacked Dec 06, 2005 2:34 PM
# re: DevSource Article on Exceptions
Thanks Tod. I fixed the link.

What do you have to say?

(will show your gravatar)
Please add 1 and 2 and type the answer here: