导航

unhandledException

Posted on 2017-08-20 00:28  清浅ヾ  阅读(177)  评论(0编辑  收藏  举报

处理未捕获的异常是每个应用程序起码有的功能,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 }