.NET Tip: Throwing Exceptions: Will You be Able to Catch What You Expect?

You know you should use try/catch blocks so that you can handle any exceptions that may occur, but are you sure you have the correct information in the exception? If you catch exceptions and then bubble them up to other parts of your application, you need to ensure that you maintain the information from the original exception. You may have some code like the following somewhere in your application:

try
{
   // Call to another function that causes an exception
}
catch (Exception ex)
{
   // Do something with the exception here

   // Bubble the exception up to the calling function
   throw ex;
}

First, let me say that you usually should not catch general exceptions like this. Instead of using the Exception class, you should catch exceptions specific to the type of work your code is doing in the try block. Your code will be much easier to follow and you can take different actions based on the type of exception. For this example, though, using the Exception class will get my point across. Although this code looks perfectly reasonable, it may not be bubbling the information you expect up to the rest of the application. If you look at the details of the exception inside the catch block, you will be able to see the name of the function where the exception occurred.

So far so good. The problem comes when you use throw ex; to bubble up the exception. By doing it this way, you will lose the stack information from the original exception so your code won’t know where it originated. Instead, the code that catches your exception will think the exception originated in your function. I’ve found that this is not usually the behavior you are expecting. One simple change, however, can fix this problem. By changing the throw ex; line to only throw; as shown below, you will maintain the original stack information.

try
{
   // Call to another function that causes an exception
}
catch (Exception ex)
{
   // Do something with the exception here

   // Bubble the exception up to the calling function
   throw;
}

This will cause the original exception to be rethrown instead of a new exception being thrown. The stack information is maintained and it won’t appear that your code is the cause of the exception. There is also a means to throw a new exception while keeping the information from the original exception. I’ll address that technique in a future tip. So for now, make sure you know what exception information will get thrown when you bubble exceptions up through your application.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read