While exception log investigation I've faced a strange behavior for exception callstack when using throw;
for re-throwing catched exception.
For example
string callstack1, callstack2;
try
{
try
{
// some code throwing exception
}
catch (Exception ex1)
{
// deal with exception
callstack1 = ex1.StackTrace;
throw;
}
}
catch (Exception ex2)
{
// handle exception
callstack2 = ex2.StackTrace;
}
In this example I was expecting the same value for callstack1
and callstack2
variables. But saved callstack information pointed me at throw;
instruction inside catch
block.
It could be expected behavior if I use throw ex1;
, but I don't.
I've used the same simple code to check this behavior on two remote computer, but I got different results. The first one works as expected (the same callstack for original and re-throw exception). The second one displays completely different callstack for re-throw exception so there is no chance to get original stack trace.
Did anyone faced this problem before?
And the main question is how to preserve original callstack when re-throwing exception?
Thank you in advance!