AppDomain.UnhandledException 事件

定義

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

public:
 virtual event UnhandledExceptionEventHandler ^ UnhandledException;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event UnhandledExceptionEventHandler UnhandledException;
member this.UnhandledException : UnhandledExceptionEventHandler 
Public Custom Event UnhandledException As 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.

注意

UnhandledException 事件的事件處理常式正在執行時,common language runtime 會暫停執行緒中止。The 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.NET Framework 4開始,除非事件處理常式是安全性關鍵,而且具有 HandleProcessCorruptedStateExceptionsAttribute 屬性,否則不會針對損毀進程狀態的例外狀況引發此事件(例如堆疊溢位或存取違規)。Starting with the .NET Framework 4.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 開始,已移除子執行緒中未處理之例外狀況的這個系所,因為這類無訊息失敗的累計效果包括效能降低、資料損毀和鎖定,這些都不容易debug.exe.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.

若要註冊此事件的事件處理常式,您必須擁有必要的許可權,否則會擲回 SecurityExceptionTo 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 方法,在連接 ThreadException 事件處理常式之前,將模式變更為 UnhandledExceptionMode.ThrowExceptionThis 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.

安全性

SecurityCriticalAttribute
需要完全信任立即呼叫者。requires full trust for the immediate caller. 部分信任或透明的程式碼無法使用這個成員。This member cannot be used by partially trusted or transparent code.

適用於