1

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!

Andre D
  • 19
  • 2
  • Maybe you shouldn't be drawing conclusions form online resources that dont guarantee at all a correct implementation. Building and running this locally with Net Core SDK 2.2 gives the expected results. – InBetween Jan 12 '19 at 17:47
  • Thank you for answer. But the online resources was used to demonstrate different behavior. Te problem described was faced with locally compiled application (VS2012). I've tried to change .NET framework version as well as target platform and Debug/Release configuration. All the time original stacktrace was lost. This will prevent me from find proper line of code reading client logs. – Andre D Jan 12 '19 at 18:12
  • Can you please describe in much more detail how it is you executed the code where you got the unexpected result? If your question is "why does dot net fiddle do the wrong thing?" take it up with them. – Eric Lippert Jan 12 '19 at 18:22
  • My question is not about "dot net fiddle", but about "how to preserve original callstack when re-throwing exception?" – Andre D Jan 12 '19 at 18:54
  • Just to clarify. I was expecting the same callstack information then using `throw;` as described [here](https://stackoverflow.com/a/178464/10905249) and [here](https://learn.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details?view=vs-2017) – Andre D Jan 12 '19 at 19:05
  • The documentation for Exception.StackTrace (https://learn.microsoft.com/en-us/dotnet/api/system.exception.stacktrace?view=netframework-4.7.1#System_Exception_StackTrace) is rather clear in its explanation what information the StackTrace contains depending on the circumstances how exactly it is being rethrown. (1/2) –  Jan 12 '19 at 21:23
  • 1
    (2/2) Note that you can preserve the stacktrace based on the accepted answer to this question: https://stackoverflow.com/questions/57383/how-to-rethrow-innerexception-without-losing-stack-trace-in-c. Following that answer doesn't mean you get the same/identical stacktrace; you will rather get the full original stacktrace + additional stacktrace entries from the rethrow. But that should hopefully meet your needs. –  Jan 12 '19 at 21:23

0 Answers0