Keeping Exception.StackTrace Intact when Rethrowing an Exception

.NET Programming

Nick Weinholt (.Net Performance guru and MVP) has an article on the correct method for rethrowing an exception in C# so that the Exception.StackTrace actually points to the root problem.

In C# the correct method for rethowing an exception is using throw; without any arguments within the catch block:

try {
// code
}
catch (Exception ex) {
throw; // Rethrow exception without resetting StackTrace
}

In VB.Net the method is the same:

Try
' code

Catch ex As Exception
Throw ' Rethrow exception without resetting StackTrace

End Try

Using this method the stack trace will go all the way down to the original exception, and not just to your catch block.



Share
  • Facebook
  • Google Bookmarks
  • Ask
  • LinkedIn
  • Socialogs
  • Wikio
  • Digg
  • Twitter
  • SlashDot
  • Reddit
  • MySpace
  • Del.icio.us
  • Blogter
  • BlogMemes
  • Yahoo Buzz






Trackback

Trackback URL for this entry: http://www.julian-kuiters.id.au/trackback.php/dotnet-throw

Here's what others have to say about 'Keeping Exception.StackTrace Intact when Rethrowing an Exception':

discus cichlids from discus cichlids
It is getting increasingly easier to keep Discus Aquarium Fish with new technology to improve water quality and Discus Fish health which could be the cause of the rise in demand. New dealers and breeders are growing rapidly and even small aquatic shops... [read more]
Tracked on Wednesday, May 05 2010 @ 05:49 PM EST

Keeping Exception.StackTrace Intact when Rethrowing an Exception | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.


Gold Coast Aquarium Maintenance | Gold Coast Marine Fish | Gold Coast Tropical Fish
Jewel Jones - Counselling Service - Penrith, Richmond
 
     
 Copyright © 2012 Julian Kuiters
 All trademarks and copyrights on this page are owned by their respective owners.