UnhandledExceptionEventArgs.ExceptionObject UnhandledExceptionEventArgs.ExceptionObject UnhandledExceptionEventArgs.ExceptionObject UnhandledExceptionEventArgs.ExceptionObject Property

Definizione

Ottiene l'oggetto eccezione non gestito.Gets the unhandled exception object.

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 gestito.The unhandled exception object.

Esempi

Nell'esempio seguente viene illustrato il UnhandledException evento.The following example demonstrates the UnhandledException event. Definisce un gestore eventi, MyHandler, che viene richiamato ogni volta che viene generata un'eccezione non gestita nel dominio applicazione predefinito.It defines an event handler, MyHandler, that is invoked whenever an unhandled exception is thrown in the default application domain. Genera quindi le due eccezioni.It then throws two exceptions. Il primo è gestito da un try/catch blocco.The first is handled by a try/catch block. La seconda non viene gestita e richiama il MyHandle routine prima che l'applicazione viene terminata.The second is unhandled and invokes the MyHandle routine before the application terminates.

// 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;
using System.Security.Permissions;

public class Example 
{
   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
   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()  
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é una derivata da Exception.This property returns an object of type Object rather than one derived from Exception. Anche se Common Language Specification richiede che tutti i tipi di eccezione derivano da Exception, è possibile chiamare i metodi generare eccezioni con gli oggetti non derivati da Exception.Although the Common Language Specification requires that all exception types derive from Exception, it is possible for methods to throw exceptions with objects not derived from Exception. È possibile eseguire le operazioni seguenti per lavorare con questa eccezione:You can do the following to work with this exception:

Si applica a

Vedi anche