Exceptions happen. It could be due to programming error, a server being offline or maybe your permissions to read a file were revoked. We need to make sure our application handles the exception and log it so we can analyze it later.

What’s worse than an exception? An exception without a stack trace.

In most cases we can execute code in the catch block to cleanup and log. However, a multi-threaded application may need to clean up other threads outside the catch block before throwing the exception. If you are not careful you might lose the original stack trace. Luckily the .NET framework provides the ExceptionDispatchInfo class. That class gives you the ability to throw the exception from outside the catch and preserve the original stack trace.

This simple example illustrates the basic usage of the ExceptionDispatchInfo class.

Leave a Reply

Your email address will not be published. Required fields are marked *