处理未捕获的异常是每个应用程序起码有的功能,C#在AppDomain提供了UnhandledException 事件来接收未捕获到的异常的通知。常见的应用如下:
1 static void Main(string[] args) 2 { 3 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 4 } 5 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 6 { 7 Exception error = (Exception)e.ExceptionObject; 8 Console.WriteLine("MyHandler caught : " + error.Message); 9 }