. net未处理异常

本文关键字:异常 未处理 net | 更新日期: 2023-09-27 18:13:21

我使用这篇MSDN文章中的示例代码。

[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);
   }

处理器捕获未处理的异常。但是,在处理程序运行之后,仍然显示未处理的异常2。在调试模式下,在发布模式下,如果.exe被直接启动。

我想抑制第二个异常,这样未处理的异常处理程序就可以静默地终止/重新启动应用程序。我记得这是在。net 3中使用VB.NET实现的。

. net未处理异常

查看问题链接中的代码:

// The example displays the following output:
//       Catch clause caught : 1
//       
//       MyHandler caught : 2
//       Runtime terminating: True
//       
//       Unhandled Exception: System.Exception: 2
//          at Example.Main()  
如您所见,没有人处理第二个异常。引用文章:

此事件提供未捕获异常的通知。它允许应用程序在系统默认处理程序向用户报告异常并终止应用程序之前记录有关异常的信息