57 out of 78 rated this helpful - Rate this topic

Application.DispatcherUnhandledException Event

Occurs when an exception is thrown by an application but not handled.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
public event DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException
<object DispatcherUnhandledException="DispatcherUnhandledExceptionEventHandler" .../>

By default, Windows Presentation Foundation (WPF) catches unhandled exceptions, notifies users of the exception from a dialog box (from which they can report the exception), and automatically shuts down an application.

However, if an application needs to perform custom unhandled exception processing from a centralized location, you should handle DispatcherUnhandledException.

DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread.

If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or a background worker thread (a thread without a Dispatcher), the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised. In these circumstances, you will need to write code to do the following:

  1. Handle exceptions on the background thread.

  2. Dispatch those exceptions to the main UI thread.

  3. Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.

For more information, see the Threading Model overview.

The DispatcherUnhandledException event handler is passed a DispatcherUnhandledExceptionEventArgs argument that contains contextual information regarding the exception, including:

You can use this information to determine whether an exception is recoverable or not. A recoverable exception might be a FileNotFoundException, for example, while an unrecoverable exception might be a StackOverflowException, for example.

When you process an unhandled exception from DispatcherUnhandledException, and you don't want WPF to continue processing it, you need to set the Handled property to true.

Unlike the other events that Application raises, DispatcherUnhandledException does not have a matching protected virtual implementation (OnDispatcherUnhandledException). Consequently, classes that derive from Application must always register an event handler with DispatcherUnhandledException to process unhandled exceptions.

The following example shows how to process unhandled exceptions by handling the DispatcherUnhandledException event.

using System.Windows; // Application 
using System.Windows.Threading; // DispatcherUnhandledExceptionEventArgs 

namespace SDKSample
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // Process unhandled exception


...


            // Prevent default unhandled exception processing
            e.Handled = true;
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.