Click here to Skip to main content
15,881,204 members
Articles / Desktop Programming / WPF

Order in Chaos: Handling Unhandled Exceptions in a WPF Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Sep 2011Ms-PL3 min read 17.9K   3  
Handling unhandled exceptions in a WPF application

Introduction

So you want to handle somehow all the unhandled exceptions in your application.
Usually you want to accomplish one of the following:

  • Log the exception for later diagnostics
  • Present the user an unhandled exception UI, nicer than the default

You heard there’s an event you should register, or maybe you find one by mistake, but is it the correct one?

Did you know there are four (!) different events for handling unhandled exceptions in the .NET Framework?

So what is the difference between them and when should we use each one? This post will hopefully answer these questions.

Note: This is NOT a replacement for try-catch blocks!

Summary

I’ll begin with the summary to avoid boring busy developers.

In a typical WPF application, you should use Application.Current.DispatcherUnhandledException for exceptions generated on the UI thread and AppDomain.CurrentDomain.UnhandledException for all the other exceptions.

Now, for the details...

System.Windows.Forms.Application.ThreadException

This event is used for catching unhandled exceptions only on UI threads created by WinForms. Other exceptions, generated on non-UI threads won’t arrive to this event. Use AppDomain.Current.UnhandledException to catch them.

The default behavior of a WinForms application with an unhandled exception is to present the following error dialog:

image

If you register to this event, the application will not show the error dialog and will automatically extend the application life (i.e., the application won’t be killed).

If you do want to end your application, maybe after logging the exception or asking the user with a personalized dialog, you must do it yourself using Application.Exit().

You can find the MSDN documentation on this event here.

System.Windows.Application.Current.DispatcherUnhandledException

This event is used for catching unhandled exceptions only from the main UI thread created by WPF.

The default behavior of a WPF application with an unhandled exception is to present the following error dialog and end the application:

image

If you register to this event, you will get the chance to log the exception, but the application will still end, unless you set e.Handled = true, on the event’s EventArgs parameter.

You can find the MSDN documentation on this event here.

Dispatcher.UnhandledException

This event is used for catching unhandled exceptions on the thread attached to the specific Dispatcher (WPF only). Note that in WPF, two threads can have two different Dispatcher objects attached. This event is only useful if you have several UI threads in your WPF application, which is quite rare. If you’re not sure, you probably need to handle only the previous event: Application.Current.DispatcherUnhandledException.

As in the previous event, if you register, you will get the change to log the exception. To prevent the exception internal handling from being called, set e.Handled = true.

You can find the MSDN documentation on this event here.

AppDomain.CurrentDomain.UnhandledException

This event is used for catching unhandled exceptions generated from all threads running under the context of a specific application domain.

You can find the MSDN documentation on this event here.

Bonus: AppDomain.CurrentDomain.FirstChanceException

This event which exists only from .NET 4, is raised on ANY exception, if they handled one. In fact, the event is raised before the search for the catch blocks. You can’t handle the exception using this event. You can use it if you need to log exceptions that are caught.

You can find the MSDN documentation on this event here.

That's it for now,
Arik Poznanski.Image 3

Image 4 Image 5 Image 6

Image 7

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
-- There are no messages in this forum --