locked
Why does Exception.StackTrace disappear?

    Question

  • I'm attempting to catch unhandled exceptions in my app on windows 8.1

    I've noticed that in my App_UnhandledException handler, that the UnhandledExceptionArgs.Exception.StackTrace is valid on entry.

    However, I take the same Exception object, and pass it to a function, and the value mysteriously becomes null.

    As a workaround, I capture the stack trace into a string variable before any other operations on the exception object, but I don't understand why this should be necessary.


    Anthony Wieser | Wieser Software Ltd | www.wieser-software.com

    Saturday, April 11, 2015 8:47 AM

Answers

  • For some reason, it seems the getter of the 'Exception' property of the 'UnhandledExceptionEventArgs' returns the original exception the first time (I've checked, it's the exact same reference), and creates a new exception the subsequent times. I don't know where this behavior comes from, but instead of saving the stacktrace you should keep a reference to the whole exception:

    var exception = e.Exception;
    
    // Now inspect exception instead of e.Exception

    Saturday, April 11, 2015 10:19 AM