.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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds