AppDomain.UnhandledException 事件

定義

發生於未攔截到例外狀況時。Occurs when an exception is not caught.

public:
 event UnhandledExceptionEventHandler ^ UnhandledException;
public:
 virtual event UnhandledExceptionEventHandler ^ UnhandledException;
public event UnhandledExceptionEventHandler? UnhandledException;
public event UnhandledExceptionEventHandler UnhandledException;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event UnhandledExceptionEventHandler UnhandledException;
member this.UnhandledException : UnhandledExceptionEventHandler 
[<add: System.Security.SecurityCritical>]
[<remove: System.Security.SecurityCritical>]
member this.UnhandledException : UnhandledExceptionEventHandler 
Public Custom Event UnhandledException As UnhandledExceptionEventHandler 

事件類型

UnhandledExceptionEventHandler

實作

屬性

範例

下列範例示範 UnhandledException 事件。The following example demonstrates the UnhandledException event. 它會定義事件處理常式, MyHandler 這是在預設應用程式域中擲回未處理的例外狀況時叫用的事件處理常式。It defines an event handler, MyHandler, that is invoked whenever an unhandled exception is thrown in the default application domain. 然後,它會擲回兩個例外狀況。It then throws two exceptions. 第一個是由 try/catch 區塊處理。The first is handled by a try/catch block. 第二個未處理,並在 MyHandle 應用程式終止之前叫用常式。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()

備註

此事件提供未攔截例外狀況的通知。This event provides notification of uncaught exceptions. 它可讓應用程式在系統預設處理常式向使用者回報例外狀況之前記錄例外狀況的相關資訊,並結束應用程式。It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. 如果有足夠的應用程式狀態相關資訊可供使用,則可能會採取其他動作,例如儲存程式資料以供稍後復原。If sufficient information about the state of the application is available, other actions may be undertaken - such as saving program data for later recovery. 請注意,因為未處理例外狀況時,程式資料可能會損毀。Caution is advised, because program data can become corrupted when exceptions are not handled.

注意

在 .NET Framework 1.0 和1.1 版中,應用程式終止和偵錯工具選項會在引發此事件之前回報給使用者,而不是在之後。In the .NET Framework versions 1.0 and 1.1, application termination and debugging options are reported to the user before this event is raised, rather than after.

此事件可以在任何應用程式域中處理。This event can be handled in any application domain. 不過,在發生例外狀況的應用程式域中,不一定會引發事件。However, the event is not necessarily raised in the application domain where the exception occurred. 只有當執行緒的整個堆疊已在未尋找適用的例外狀況處理常式的情況下進行展開時,才會發生未處理的例外狀況,因此可以引發事件的第一個位置是在發出執行緒的應用程式域中。An exception is unhandled only if the entire stack for the thread has been unwound without finding an applicable exception handler, so the first place the event can be raised is in the application domain where the thread originated.

注意

在 .NET Framework 1.0 和1.1 版中,只有當應用程式啟動時,系統建立的預設應用程式域才會發生此事件。In the .NET Framework versions 1.0 and 1.1, this event occurs only for the default application domain that is created by the system when an application is started. 如果應用程式建立額外的應用程式域,在這些應用程式域中指定這個事件的委派就不會有任何作用。If an application creates additional application domains, specifying a delegate for this event in those applications domains has no effect.

如果 UnhandledException 事件是在預設應用程式域中處理,不論執行緒在哪一個應用程式域中啟動,都會在任何執行緒中引發任何未處理的例外狀況。If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in. 如果執行緒在具有事件處理常式的應用程式域中啟動 UnhandledException ,則該事件會在該應用程式域中引發。If the thread started in an application domain that has an event handler for UnhandledException, the event is raised in that application domain. 如果該應用程式域不是預設的應用程式域,而且預設的應用程式域中也有一個事件處理常式,則會在這兩個應用程式域中引發此事件。If that application domain is not the default application domain, and there is also an event handler in the default application domain, the event is raised in both application domains.

例如,假設某個執行緒在應用程式域 "AD1" 中啟動,呼叫應用程式域 "AD2" 中的方法,然後從該處呼叫應用程式域 "AD3" 中的方法,它會擲回例外狀況。For example, suppose a thread starts in application domain "AD1", calls a method in application domain "AD2", and from there calls a method in application domain "AD3", where it throws an exception. 可以引發事件的第一個應用程式域 UnhandledException 是 "AD1"。The first application domain in which the UnhandledException event can be raised is "AD1". 如果該應用程式域不是預設的應用程式域,則事件也可以在預設的應用程式域中引發。If that application domain is not the default application domain, the event can also be raised in the default application domain.

注意

當事件的事件處理常式正在執行時,common language runtime 會暫停執行緒中止 UnhandledExceptionThe common language runtime suspends thread aborts while event handlers for the UnhandledException event are executing.

如果事件處理常式具有 ReliabilityContractAttribute 具有適當旗標的屬性,則會將事件處理常式視為受限制的執列區域。If the event handler has a ReliabilityContractAttribute attribute with the appropriate flags, the event handler is treated as a constrained execution region.

從 .NET Framework 4 開始,除非事件處理常式是安全性關鍵,而且具有屬性,否則不會針對損毀進程狀態的例外狀況(例如堆疊溢位或存取違規)引發此事件 HandleProcessCorruptedStateExceptionsAttributeStarting with the .NET Framework 4, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the HandleProcessCorruptedStateExceptionsAttribute attribute.

在 .NET Framework 1.0 和1.1 版中,執行時間會捕捉到主應用程式執行緒以外的執行緒中發生的未處理例外狀況,因此不會導致應用程式終止。In the .NET Framework versions 1.0 and 1.1, an unhandled exception that occurs in a thread other than the main application thread is caught by the runtime and therefore does not cause the application to terminate. 因此,在 UnhandledException 沒有應用程式終止的情況下,可能會引發事件。Thus, it is possible for the UnhandledException event to be raised without the application terminating. 從 .NET Framework 版本2.0 開始,已移除子執行緒中未處理之例外狀況的這項功能,因為這種無訊息失敗的累積效果包括效能降低、損毀的資料和鎖定,全都難以進行調試。Starting with the .NET Framework version 2.0, this backstop for unhandled exceptions in child threads was removed, because the cumulative effect of such silent failures included performance degradation, corrupted data, and lockups, all of which were difficult to debug. 如需詳細資訊(包括執行時間不會終止的案例清單),請參閱 Managed 執行緒中的例外狀況。For more information, including a list of cases in which the runtime does not terminate, see Exceptions in Managed Threads.

若要註冊這個事件的事件處理常式,您必須擁有必要的許可權,否則 SecurityException 會擲回。To register an event handler for this event, you must have the required permissions, or a SecurityException is thrown.

如需處理事件的詳細資訊,請參閱 處理和引發事件For more information about handling events, see Handling and Raising Events.

未處理例外狀況的其他事件Other Events for Unhandled Exceptions

針對某些應用程式模型, UnhandledException 如果在主應用程式執行緒中發生未處理的例外狀況,則事件可以被其他事件佔用。For certain application models, the UnhandledException event can be preempted by other events if the unhandled exception occurs in the main application thread.

在使用 Windows Forms 的應用程式中,主要應用程式執行緒中未處理的例外狀況會 Application.ThreadException 引發事件。In applications that use Windows Forms, unhandled exceptions in the main application thread cause the Application.ThreadException event to be raised. 如果處理此事件,則預設行為是未處理的例外狀況不會終止應用程式,但應用程式仍處於未知的狀態。If this event is handled, the default behavior is that the unhandled exception does not terminate the application, although the application is left in an unknown state. 在此情況下, UnhandledException 不會引發事件。In that case, the UnhandledException event is not raised. 您可以使用應用程式佈建檔來變更此行為,或使用方法在連接 Application.SetUnhandledExceptionMode UnhandledExceptionMode.ThrowException ThreadException 事件處理常式之前,將模式變更為。This behavior can be changed by using the application configuration file, or by using the Application.SetUnhandledExceptionMode method to change the mode to UnhandledExceptionMode.ThrowException before the ThreadException event handler is hooked up. 這只適用于主要應用程式執行緒。This applies only to the main application thread. UnhandledException 其他執行緒中擲回未處理的例外狀況時,就會引發此事件。The UnhandledException event is raised for unhandled exceptions thrown in other threads.

從 Microsoft Visual Studio 2005 開始,Visual Basic 應用程式架構會針對主應用程式執行緒中未處理的例外狀況提供另一個事件。Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread. 查看 WindowsFormsApplicationBase.UnhandledException 事件。See the WindowsFormsApplicationBase.UnhandledException event. 這個事件有一個事件引數物件,其名稱與所使用的事件引數物件相同 AppDomain.UnhandledException ,但具有不同的屬性。This event has an event arguments object with the same name as the event arguments object used by AppDomain.UnhandledException, but with different properties. 特別是,這個事件引數物件的 ExitApplication 屬性可讓應用程式繼續執行,並忽略未處理的例外狀況 (,並讓應用程式處於未知的狀態) 。In particular, this event arguments object has an ExitApplication property that allows the application to continue running, ignoring the unhandled exception (and leaving the application in an unknown state). 在此情況下, AppDomain.UnhandledException 不會引發事件。In that case, the AppDomain.UnhandledException event is not raised.

適用於