Re-throwing an Exception in .NET

Provided by Dave Juth, Senior Systems Architect

The stack trace is helpful when you are debugging your application. What is not readily apparent is the code you put in place may cause the stack trace to be cleared.

It may be typical for you to write the following error handling:

 

   //C#

   try

   {

      // some code executed here

   }

    

   catch (Exception ex)

   {

      throw ex;

   }

 

 

   'VB

   Try

     ' some code here

 

   Catch ex As Exception

     Throw ex

    

   End Try

 

In the above example, the origin of the exception becomes the line "throw ex." This hides the real location of the exception, and makes debugging a guessing game.
The proper way to re-throw an exception is to use the 'Throw' statement with no exception argument.

 

   //C#

   try

   {

      // some code here

   }

    

   catch (Exception ex)

   {

      throw;

   }  

  

  

   'VB

   Try

      ' some code here

    

   Catch

      Throw

    

   End Try


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS