Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Order in Chaos: Handling unhandled exceptions in a WPF application

DZone's Guide to

Order in Chaos: Handling unhandled exceptions in a WPF application

·
Free Resource

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 object 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 the 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.

Topics:

Published at DZone with permission of Arik Poznanski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}