Dealing with unhandled exceptions in WPF

Just like my previous post on dealing with unhandled exceptions in WinForms, I’ll be talking about how to accomplish the same thing in WPF. When your WPF application encounters an unhandled exception, the default .NET exception handler for WPF displays the following dialogs to the user.

There is not much information in these dialogs that would tell the user what went wrong. More importantly there is very little information for you as the developer in these dialogs that would help you find out what went wrong. Without stating everything I said in my previous post all over again, I’ll just say that there is a better option by replacing the default unhandled exception handler with your own.

My same disclaimer applies to this post as well. I grabbed most of the samples your about to see from MSDN (with some tweaks). However it was much harder to find them for WPF, and I had to combine two different sample apps to get you the complete solution for UI thread exception handling and non-UI thread exception handling.

Unlike the WinForm example, where we had to register for two events, there is only one DispatcherUnhandledException event that we have to be concerned about in WPF. You register your handler for this in the App.xaml.

1: <Application

2: x:Class="DispatcherUnhandledExceptionSample.App"

3: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

4: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

5: StartupUri="MainWindow.xaml"

6: DispatcherUnhandledException="App_DispatcherUnhandledException" />

Next lets look at our code-behind for App.xaml and the handler function App_DispatcherUnhandledException().

There is not much required of this handler function other than the last line of code on line 31. We set the handled property to true so that the .NET framework does not display its own handler for the exception. If we were only concerned with exceptions that occurred on the UI thread, that would be all that is to it!

Next let’s look at how we can also handle exceptions that occur in other worker threads by using a Dispatcher. I’ve created a simple form, similar to the previous WinForm example, that has two buttons on it – one for generating an exception on the UI thread and another that generates an exception on a worker thread. Here are the important functions on that form:

Where all of the trickery happens is in the MethodRunningOnSecondaryWorkerThread() function. Here we have a try catch block with a generic exception handler (code smell – I know, but there is no way around it) that wraps the execution of the WorkerMethod(). After we catch the exception, we invoke a Dispatcher to execute some code on the main UI thread. All we do is re-throw the exception and our handler for the DispatcherUnhandledException event, in App.xaml, will catch the error. That’s it.

This entry was posted
on Friday, April 17th, 2009 at 6:52 am and is filed under WPF.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.