Monday, February 1, 2010

Preserving the complete stack trace when rethrowing an exception

Even throw; loses the top level of the stack trace. Use this 'hack' to work around the problem:

static void PreserveStackTrace(Exception exception)
{
    MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
        BindingFlags.Instance | BindingFlags.NonPublic);
    preserveStackTrace.Invoke(exception, null);
}

(from here)

0 comments:

Post a Comment