Seattle CodeCamp & Exceptions

In Development by PaulLeave a Comment

I went to Seattle CodeCamp last weekend. I learned about a lot of new technologies (to me) including React.js, Azure Machine Learning, and Ionic Framework. I’m planning to investigate those more in the future. But one of my favorite sessions was Adam Furmanek’sInternals of Exceptions” talk. He had the most practical (and immediately useful) tidbit that I’d heard all day: he had a better way to rethrow exceptions in C#. I’d been using “throw” for a long time:

try
{
    …
}
catch(Exception e)
{
    …
    throw;
}

But I noticed that I didn’t always get the entire stack trace. I didn’t think much of it, because I wasn’t aware of a better solution. But Adam had a better solution:

try
{
    …
}
catch(Exception e)
{
    …
    ExceptionDispatchInfo.Capture(e).Throw();
}

That keeps more of the stack trace intact. I’ll definitely be doing it this way in the future!

Share:

Leave a Comment