UnhandledExceptionEventArgs.ExceptionObject Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene l'oggetto eccezione non gestita.
public:
property System::Object ^ ExceptionObject { System::Object ^ get(); };
public object ExceptionObject { get; }
member this.ExceptionObject : obj
Public ReadOnly Property ExceptionObject As Object
Valore della proprietà
Oggetto eccezione non gestita.
Esempio
Nell'esempio seguente viene illustrato l'evento UnhandledException . Definisce un gestore eventi, MyHandler, che viene richiamato ogni volta che viene generata un'eccezione non gestita nel dominio applicazione predefinito. Genera quindi due eccezioni. Il primo viene gestito da un blocco try/catch . Il secondo non è gestito e richiama la routine prima che MyHandle l'applicazione termini.
// The example should be compiled with the /clr:pure compiler option.
using namespace System;
using namespace System::Security::Permissions;
public ref class Example
{
private:
static void MyHandler(Object^ sender, UnhandledExceptionEventArgs^ args)
{
Exception^ e = dynamic_cast<Exception^>(args->ExceptionObject);
Console::WriteLine( "MyHandler caught : {0}", e->Message );
Console::WriteLine("Runtime terminating: {0}", args->IsTerminating);
}
public:
[SecurityPermissionAttribute( SecurityAction::Demand, ControlAppDomain = true )]
static void Main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
currentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler(Example::MyHandler);
try
{
throw gcnew Exception("1");
}
catch (Exception^ e)
{
Console::WriteLine( "Catch clause caught : {0}\n", e->Message );
}
throw gcnew Exception("2");
}
};
void main()
{
Example::Main();
}
// The example displays the following output:
// Catch clause caught : 1
//
// MyHandler caught : 2
// Runtime terminating: True
//
// Unhandled Exception: System.Exception: 2
// at Example.Main()
// at mainCRTStartup(String[] arguments)
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()
Commenti
Questa proprietà restituisce un oggetto di tipo Object anziché un oggetto derivato da Exception. Sebbene common Language Specification richieda che tutti i tipi di eccezione derivano da Exception, è possibile che i metodi generino eccezioni con oggetti non derivati da Exception. Per usare questa eccezione, è possibile eseguire le operazioni seguenti:
Applicare l'attributo RuntimeCompatibilityAttribute con un RuntimeCompatibilityAttribute.WrapNonExceptionThrows valore di
trueall'assembly che contiene il gestore eventi. In questo modo tutte le eccezioni non derivate dalla Exception classe in un RuntimeWrappedException oggetto . È quindi possibile eseguire il cast sicuro (in C#) o convertire (in Visual Basic) l'oggetto restituito da questa proprietà in un Exception oggetto e recuperare l'oggetto eccezione originale dalla RuntimeWrappedException.WrappedException proprietà . Si noti che alcuni compilatori, ad esempio i compilatori C# e Visual Basic, applicano automaticamente questo attributo.Eseguire il cast dell'oggetto restituito da questa proprietà a un Exception oggetto .