.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.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • This report outlines the future look of Forrester's solution for security and risk (S&R) executives working on building an identity and access management strategy for the extended enterprise. We designed this report to help you understand and navigate the major business and IT trends affecting identity and access management (IAM) during the next five years. IAM in 2012 has become a tool not just for security but also for business agility. Competitive challenges push businesses into the cloud and encourage …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date