Search code examples
c#.netwpfexceptionunhandled-exception

Difference between UnhandledException and DispatcherUnhandledException in .NET


What is the difference between AppDomain.UnhandledException and Application.DispatcherUnhandledException in .NET?

I need an event that is fired when any unhandled exception occurs. I have come across these two, but I dont know in what ways they differ from each other. Also, are there cases when they are not fired?


Solution

  • Application.DispatcherUnhandledException will handle exceptions thrown on the main UI thread in a WPF application. AppDomain.UnhandledException will handle exceptions thrown on any thread and never caught. This includes threads you create manually or the main thread in a Console application. WPF is catching the exceptions on the UI thread, so you will not see those in AppDomain.UnhandledException.

    Also note that unhandled exceptions typically terminate the runtime, so after AppDomain.UnhandledException is raised your program will immediately exit. In contrast, Application.DispatcherUnhandledException is catching exceptions and will let your program continue.