Application..::.DispatcherUnhandledException Event Home
This page is specific to:.NET Framework Version:3.03.54.0
.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
Syntax

'Usage

Dim instance As Application
Dim handler As DispatcherUnhandledExceptionEventHandler

AddHandler instance.DispatcherUnhandledException, handler


'Declaration

Public Event DispatcherUnhandledException As DispatcherUnhandledExceptionEventHandler
<object DispatcherUnhandledException="DispatcherUnhandledExceptionEventHandler" .../>
Remarks

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.

Examples

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


Platforms

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.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Community Content

Managing Unhandled Exceptions on Secondary Worker Threads Sample
Added by:Frank Dzaebel

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.
_______

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View