UnhandledExceptionEventArgs.ExceptionObject Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient l’objet d’exception non géré.
public:
property System::Object ^ ExceptionObject { System::Object ^ get(); };
public object ExceptionObject { get; }
member this.ExceptionObject : obj
Public ReadOnly Property ExceptionObject As Object
Valeur de propriété
Objet d’exception non géré.
Exemples
L’exemple suivant illustre l’événement UnhandledException . Il définit un gestionnaire d’événements, MyHandlerqui est appelé chaque fois qu’une exception non gérée 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 . La seconde est non gérée et appelle la routine avant la MyHandle fin de l’application.
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()
open System
open System.Security.Permissions
let myHandler _ (args: UnhandledExceptionEventArgs) =
let e = args.ExceptionObject :?> Exception
printfn $"MyHandler caught : {e.Message}"
printfn $"Runtime terminating: {args.IsTerminating}"
[<EntryPoint>]
let main _ =
let currentDomain = AppDomain.CurrentDomain
currentDomain.UnhandledException.AddHandler(UnhandledExceptionEventHandler myHandler)
try
failwith "1"
with e ->
printfn $"Catch clause caught : {e.Message} \n"
failwith "2"
// The example displays the following output:
// Catch clause caught : 1
//
// MyHandler caught : 2
// Runtime terminating: True
//
// Unhandled Exception: System.Exception: 2
// at Example.main()
Module Example
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler
Try
Throw New Exception("1")
Catch e As Exception
Console.WriteLine("Catch clause caught : " + e.Message)
Console.WriteLine()
End Try
Throw New Exception("2")
End Sub
Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
Console.WriteLine("MyHandler caught : " + e.Message)
Console.WriteLine("Runtime terminating: {0}", args.IsTerminating)
End Sub
End Module
' 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
Cette propriété retourne un objet de type Object plutôt qu’un objet dérivé de Exception. Bien que la spécification du langage commun exige que tous les types d’exceptions dérivent , il est possible que les méthodes lèvent Exceptiondes exceptions avec des objets non dérivés de Exception. Vous pouvez effectuer les opérations suivantes pour utiliser cette exception :
Appliquez l’attribut RuntimeCompatibilityAttribute avec une RuntimeCompatibilityAttribute.WrapNonExceptionThrows valeur de
truel’assembly qui contient le gestionnaire d’événements. Cela encapsule toutes les exceptions non dérivées de la Exception classe dans un RuntimeWrappedException objet. Vous pouvez ensuite convertir en toute sécurité (en C#) ou convertir (en Visual Basic) l’objet retourné par cette propriété en objet Exception et récupérer l’objet d’exception d’origine de la RuntimeWrappedException.WrappedException propriété. Notez que certains compilateurs, tels que les compilateurs C# et Visual Basic, appliquent automatiquement cet attribut.Cassez l’objet retourné par cette propriété en objet Exception .