Preserving the Stack Trace

Rethrowing Exceptions

Normally, if you have to rethrow and existing exception in .NET, you should use the throw keyword without any parameters:

try
{
    // Code that can generate exceptions here
}
catch (Exception ex)
{
    // Do some logging or whatever
    throw;
}

Using throw ex; would cause the stack trace of the exception to be lost, in which case we lose some valuable information. However, this only works inside the catch block that caught the exception in the first place.

Sometimes, you need to rethrow the exception at a later point outside the catch block, but of course we want to preserve the stack trace.

.NET Framework 4.5

If you're using .NET 4.5, you're in luck. One of the new features is built-in support for preserving the stack trace, by using the new ExceptionDispatchInfo class.

ExceptionDispatchInfo exceptionDispatchInfo = null;

try
{
    // Code that can generate exceptions here
}
catch (Exception ex)
{
    exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
}

// Do whatever in between

if (exceptionDispatchInfo != null)
    exceptionDispatchInfo.Throw();

Older Versions

If you're using a somewhat older version of the framework, you can still achieve something similar:

using System;
using System.Reflection;

static class ExceptionExtensions
{
    public static void PreserveStackTrace(this Exception exception)
    {
        MethodInfo preserveStackTrace = typeof(Exception).GetMethod(
            "InternalPreserveStackTrace",
            BindingFlags.Instance | BindingFlags.NonPublic);

        preserveStackTrace.Invoke(exception, null);
    }
}

This code uses reflection to call an internal method InternalPreserveStackTrace, which means you must run with a high enough trust level to allow for this. It's used like so:

Exception exception = null;

try
{
    // Code that can generate exceptions here
}
catch (Exception ex)
{
    ex.PreserveStackTrace();
    exception = ex;
}

// Do whatever in between

if (exception != null)
    throw exception;

Conclusions

If you're only targeting .NET 4.5, using ExceptionDispatchInfo is clearly preferred, because it does not require a high trust environment and it also stores some additional information about the exception that normally gets lost: the "Watson buckets".

If you are writing code for .NET 4.0 or earlier, but plan to move to 4.5 later, you could consider implementing your own ExceptionDispatchInfo in very much the same way as the extension method above.

Comments (1) -

  • Really interesting article, and very usefull too. I will implement this structure cause actually I have not all the trace info when catching and throwing exceptions. Thank you! Smile

Add comment

Loading