I have a WPF application and I need to write a mini dump file whenever my application encounters an unhandled exception to aid in debugging. However, every time the exception handler is called, the stack is completely unwound to the handler, and there is no useful state that I could use in the dump file.
I tried to subscribe to both of them, and the stack is unwound for both of them:
Application.Current.DispatcherUnhandledException AppDomain.CurrentDomain.UnhandledException
I tried the same with a console application and the stack does not unwind, so it is definitely related to WPF. Exception and handlers occur in the main thread.
Here are 2 sample code that you can easily see. Just set breakpoints in each handler and watch the call stack as you reach breakpoints.
Console Application:
class Program { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); int foo = 5; ++foo; throw new ApplicationException("blah"); ++foo; } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine("blah"); } }
WPF application:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); int foo = 5; ++foo; throw new ApplicationException("blah"); ++foo; base.OnStartup(e); } void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {} void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { e.Handled = false; } }
source share