Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Application Class
Application Events
 DispatcherUnhandledException Event
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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
Visual Basic (Declaration)
Public Event DispatcherUnhandledException As DispatcherUnhandledExceptionEventHandler
Visual Basic (Usage)
Dim instance As Application
Dim handler As DispatcherUnhandledExceptionEventHandler

AddHandler instance.DispatcherUnhandledException, handler
C#
public event DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException
Visual C++
public:
 event DispatcherUnhandledExceptionEventHandler^ DispatcherUnhandledException {
    void add (DispatcherUnhandledExceptionEventHandler^ value);
    void remove (DispatcherUnhandledExceptionEventHandler^ value);
}
JScript
JScript does not support events.
XAML Attribute Usage
<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.

C#
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;
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Managing Unhandled Exceptions on Secondary Worker Threads Sample      Frank Dzaebel   |   Edit   |   Show History

This following sample demonstrates how to manage unhandled exceptions with use of the DispatcherUnhandledException event that are thrown on secondary worker threads.


[Managing Unhandled Exceptions on Secondary Worker Threads Sample]
http://msdn.microsoft.com/en-us/library/bb625944.aspx

This sample demonstrates a specific feature of WPF and, consequently, does not follow application development best practices.
_______

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker