Application.DispatcherUnhandledException Événement

Définition

Se produit lorsqu'une exception est levée par une application mais non gérée.

public:
 event System::Windows::Threading::DispatcherUnhandledExceptionEventHandler ^ DispatcherUnhandledException;
public event System.Windows.Threading.DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException;
member this.DispatcherUnhandledException : System.Windows.Threading.DispatcherUnhandledExceptionEventHandler 
Public Custom Event DispatcherUnhandledException As DispatcherUnhandledExceptionEventHandler 

Type d'événement

Exemples

L’exemple suivant montre comment traiter les exceptions non gérées en gérant l’événement DispatcherUnhandledException .

using System.Windows;
using System.Windows.Threading;

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;
        }
    }
}
Imports System.Windows
Imports System.Windows.Threading

Namespace SDKSample
    Partial Public Class App
        Inherits Application
        Private Sub App_DispatcherUnhandledException(ByVal sender As Object, ByVal e As DispatcherUnhandledExceptionEventArgs)
            ' Process unhandled exception

            ' Prevent default unhandled exception processing
            e.Handled = True
        End Sub
    End Class
End Namespace

Remarques

Par défaut, Windows Presentation Foundation intercepte les exceptions non gérées, avertit les utilisateurs de l’exception à partir d’une boîte de dialogue (à partir de laquelle ils peuvent signaler l’exception) et arrête automatiquement une application.

Toutefois, si une application doit effectuer un traitement d’exception personnalisé non géré à partir d’un emplacement centralisé, vous devez gérer DispatcherUnhandledException.

DispatcherUnhandledExceptionest déclenché par un Application pour chaque exception non gérée par le code en cours d’exécution sur le thread d’interface utilisateur main.

Si une exception n’est pas gérée sur un thread d’interface utilisateur d’arrière-plan (un thread avec son propre Dispatcher) ou sur un thread de travail en arrière-plan (un thread sans Dispatcher), l’exception n’est pas transférée au thread d’interface utilisateur main. Par conséquent, DispatcherUnhandledException n’est pas soulevé. Dans ces circonstances, vous devez écrire du code pour effectuer les opérations suivantes :

  1. Gérer les exceptions sur le thread d’arrière-plan.

  2. Distribuez ces exceptions au thread d’interface utilisateur main.

  3. Relancez-les sur le thread d’interface utilisateur main sans les gérer pour permettre DispatcherUnhandledException leur levée.

Pour plus d’informations, consultez vue d’ensemble du modèle de thread.

Le DispatcherUnhandledException gestionnaire d’événements se fait passer un DispatcherUnhandledExceptionEventArgs argument qui contient des informations contextuelles concernant l’exception, notamment :

Vous pouvez utiliser ces informations pour déterminer si une exception est récupérable ou non. Une exception récupérable peut être un FileNotFoundException, par exemple, tandis qu’une exception irrécupérable peut être un StackOverflowException, par exemple.

Lorsque vous traitez une exception non gérée de DispatcherUnhandledExceptionet que vous ne souhaitez pas que WPF continue à la traiter, vous devez définir la propriété sur trueHandled .

Contrairement aux autres événements qui Application se déclenchent, DispatcherUnhandledException n’a pas d’implémentation virtuelle protégée correspondante (OnDispatcherUnhandledException). Par conséquent, les classes qui dérivent de Application doivent toujours inscrire un gestionnaire d’événements avec DispatcherUnhandledException pour traiter les exceptions non gérées.

S’applique à