Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what can lead throw to reset a callstack (I'm using "throw", not "throw ex")

I've always thought the difference between "throw" and "throw ex" was that throw alone wasn't resetting the stacktrace of the exception.

Unfortunately, that's not the behavior I'm experiencing ; here is a simple sample reproducing my issue :

using System; using System.Text;  namespace testthrow2 {     class Program     {         static void Main(string[] args)         {             try             {                 try                 {                     throw new Exception("line 14");                 }                 catch (Exception)                 {                     throw; // line 18                 }             }             catch (Exception ex)             {                 Console.WriteLine(ex.ToString());              }             Console.ReadLine();         }     } } 

I would expect this code to print a callstack starting at line 14 ; however the callstack starts at line 18. Of course it's no big deal in the sample, but in my real life application, losing the initial error information is quite painful.

Am I missing something obvious? Is there another way to achieve what I want (ie re throwing an exception without losing the stack information?)

I'm using .net 3.5

like image 875
Brann Avatar asked Mar 01 '11 08:03

Brann


2 Answers

You should read this article:

  • Rethrowing exceptions and preserving the full call stack trace

In short, throw usually preserves the stack trace of the original thrown exception, but only if the exception didn't occur in the current stack frame (i.e. method).

There is a method PreserveStackTrace (shown in that blog article) that you use that preserves the original stack trace like this:

try {  } catch (Exception ex) {     PreserveStackTrace(ex);     throw; } 

But my usual solution is to either simply to not catch and re throw exceptions like this (unless absolutely necessary), or just to always throw new exceptions using the InnerException property to propagate the original exception:

try {  } catch (Exception ex) {      throw new Exception("Error doing foo", ex); } 
like image 121
Justin Avatar answered Sep 25 '22 10:09

Justin


The problem is that Windows is resetting the stack's starting point. The CLR is behaving as expected—this is just a limitation of the host operating system's exception handling support. The problem is that there can only be one stack frame per method call.

You could extract your exception handling routines into a separate "helper" method, which would work around the limitations imposed by Windows's SEH, but I don't think that's necessarily a good idea.

The proper way to rethrow an exception without losing the stack information is to throw a new exception and include the original, caught exception as the inner exception.

It's difficult to imagine very many cases where you'd really need to do this. If you're not handling the exception, and simply catching it to rethrow it, you probably shouldn't be catching it in the first place.

like image 40
Cody Gray Avatar answered Sep 23 '22 10:09

Cody Gray