.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

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds