Lire en anglais

Partager via


AppDomain.UnhandledException Événement

Définition

Se produit quand une exception n'est pas interceptée.

C#
public event UnhandledExceptionEventHandler? UnhandledException;
C#
public event UnhandledExceptionEventHandler UnhandledException;
C#
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event UnhandledExceptionEventHandler UnhandledException;

Type d'événement

Implémente

Attributs

Exemples

L’exemple suivant illustre l’événement UnhandledException . Il définit un gestionnaire d’événements, MyHandler, appelé chaque fois qu’une exception non prise en charge est levée dans le domaine d’application par défaut. Il lève ensuite deux exceptions. Le premier est géré par un bloc try/catch . Le second n’est pas pris en charge et appelle la MyHandle routine avant la fin de l’application.

C#
using System;

public class Example
{
   public static void Main()
   {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

      try {
         throw new Exception("1");
      } catch (Exception e) {
         Console.WriteLine("Catch clause caught : {0} \n", e.Message);
      }

      throw new Exception("2");
   }

   static void MyHandler(object sender, UnhandledExceptionEventArgs args)
   {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
      Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
   }
}
// The example displays the following output:
//       Catch clause caught : 1
//
//       MyHandler caught : 2
//       Runtime terminating: True
//
//       Unhandled Exception: System.Exception: 2
//          at Example.Main()

Remarques

Pour plus d’informations sur cette API, consultez Remarques supplémentaires sur l’API pour UnhandledException.

S’applique à

Produit Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1