AppDomain.UnhandledException 事件

当某个异常未被捕获时出现。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Event UnhandledException As UnhandledExceptionEventHandler
用法
Dim instance As AppDomain
Dim handler As UnhandledExceptionEventHandler

AddHandler instance.UnhandledException, handler
public event UnhandledExceptionEventHandler UnhandledException
public:
virtual event UnhandledExceptionEventHandler^ UnhandledException {
    void add (UnhandledExceptionEventHandler^ value) sealed;
    void remove (UnhandledExceptionEventHandler^ value) sealed;
}
/** @event */
public final void add_UnhandledException (UnhandledExceptionEventHandler value)

/** @event */
public final void remove_UnhandledException (UnhandledExceptionEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

此事件提供未捕获到的异常的通知。此事件使应用程序能在系统默认处理程序向用户报告异常并终止应用程序之前记录有关异常的信息。如果有足够的有关应用程序状态的信息,则可以采取其他措施,如保存程序数据以便于以后进行恢复。建议用户谨慎行事,因为未处理的异常可能会损坏程序数据,使其无法恢复。

提示

在 .NET Framework 1.0 和 1.1 版中,引发此事件之前会向用户报告应用程序终止和调试选项。

此事件可以在任何应用程序域中进行处理。如果在默认的应用程序域中进行处理,则会为任何应用程序域中的未处理的异常引发此事件。如果在子域中进行处理,则只会为该域中的未处理异常引发此事件。如果同时在默认域和子域中处理该事件,则子域中的未处理异常将导致在这两个域中引发此事件。

提示

在 .NET Framework 1.0 和 1.1 版中,此事件只会在启动应用程序时由系统创建的应用程序域内才会发生。如果应用程序创建其他应用程序域,那么在那些应用程序域中为此事件指定委托无效。

在 .NET Framework 1.0 和 1.1 版中,在主应用程序线程以外的线程中发生的未处理异常由运行库捕获,因此不会导致应用程序终止。这样就可能在应用程序不终止的情况下引发 UnhandledException 事件。在 .NET Framework 2.0 版中,这一对子线程中未处理异常的支持已被移除,因为这类静默失败的累积后果(包括性能降低、数据损坏及锁定)会使程序调试很难进行。有关更多信息,请参见 托管线程中的异常

若要为此事件注册事件处理程序,必须具有所需权限,否则将引发 SecurityException

有关处理事件的更多信息,请参见 使用事件

示例

下面的示例阐释 UnhandledException 事件。

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)
   End Try
   
   Throw New Exception("2")

   ' Output:
   '   Catch clause caught : 1
   '   MyHandler caught : 2
End Sub 'Main


Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
   Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
   Console.WriteLine("MyHandler caught : " + e.Message)
End Sub 'MyUnhandledExceptionEventHandler
using System;
using System.Security.Permissions;

public class Test {

   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
   public static void Example()
   {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
      
      try {
         throw new Exception("1");
      } catch (Exception e) {
         Console.WriteLine("Catch clause caught : " + e.Message);
      }

      throw new Exception("2");

      // Output:
      //   Catch clause caught : 1
      //   MyHandler caught : 2
   }
   
   static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
   }

   public static void Main() {
      Example();
   }
}
public ref class Test
{
private:
   static void MyHandler( Object^ /*sender*/, UnhandledExceptionEventArgs^ args )
   {
      Exception^ e = dynamic_cast<Exception^>(args->ExceptionObject);
      Console::WriteLine( "MyHandler caught : {0}", e->Message );
   }
public: 
   [SecurityPermissionAttribute( SecurityAction::Demand, ControlAppDomain = true )]
   static void Main()
   {
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   currentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler( Test::MyHandler );
   try
   {
      throw gcnew Exception( "1" );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Catch clause caught : {0}", e->Message );
   }

   throw gcnew Exception( "2" );
   
   // Output:
   //   Catch clause caught : 1
   //   MyHandler caught : 2
   }
};

int main()
{
   Test::Main();
}

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

AppDomain 类
AppDomain 成员
System 命名空间