.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

  • For the third consecutive year, Neustar surveyed hundreds of companies on distributed denial of service (DDoS) attacks. Neustar's survey reveals evidence that the DDoS attack landscape is changing. The number of companies attacked is up, but attack duration is down. Larger attacks are more common, but most attacks are still less than 1 Gbps. More than half of attacked companies reported theft of funds, data or intellectual property. Read the full report to learn how DDoS attacks have evolved in both strategy …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds