在哪里放置中央的错误处理程序的Windows窗体项目 [英] Where to place a central error handler for a Windows Forms project

查看:161
本文介绍了在哪里放置中央的错误处理程序的Windows窗体项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET我可以为了处理那些未处理过的任何错误使用的Application_Error Global.asax的范围内。

In ASP.NET I can use Application_Error within global.asax in order to handle any errors that have been unhandled.

有没有在Windows窗体等效?

Is there an equivalent in windows forms?

推荐答案

是其AppDomain.UnhandledException

Yes its AppDomain.UnhandledException


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

这篇关于在哪里放置中央的错误处理程序的Windows窗体项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆