【问题标题】:UnhandledException Event doesn't work?UnhandledException 事件不起作用?
【发布时间】:2013-05-19 01:06:56
【问题描述】:

我正在编写一个小库来捕获所有未处理的异常,显示一个小对话框(类似于 NF 的通常对话框),让用户有机会将异常发送给开发人员。为此,我像这样使用 AppDomain 的 UnhandledException-Event:

app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
        {
            ExceptionHandler handler = new ExceptionHandler((Exception)e.ExceptionObject, ExEntry);
            UnhandledExceptionListened(handler);
            if (Properties.Settings.Default.ShowStandardExceptionDialog)
            {
                ExceptionDialog exdialog = new ExceptionDialog(handler);
                exdialog.ShowDialog();
            }
        };

ExceptionHandler 和 ExEntry 是我的库的类。但是:如果发生异常,编译器会跳转到我的 Lambda-Expression,尝试调试第一行代码,然后显示之前发生的错误,而无需处理 lambda 的其余部分。 但如果我只是写:

 app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
        {
                ExceptionDialog exdialog = new ExceptionDialog(handler);
                exdialog.ShowDialog();
        };

完美运行。有谁知道为什么这不起作用?

【问题讨论】:

  • 将顶级代码包装在 try catch 中,并在 catch 中放置一个观察点。有没有例外,如果有,那是什么?
  • 在测试单元中简单地测试异常处理程序的创建怎么样?会发生什么?

标签: c# exception unhandled


【解决方案1】:

可能有两个原因。

一个是你没有正确设置UnhandledExceptionMode

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

另一个是你没有处理ThreadException,抛出的异常不是未处理的异常而是线程异常。

以下是一个例子,你需要根据你的场景来修改它​​:

Application.ThreadException+=
    new ThreadExceptionEventHandler(Log.WriteThreadException);

AppDomain.CurrentDomain.UnhandledException+=
    new UnhandledExceptionEventHandler(Log.WriteUnhandledException);

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

【讨论】:

    【解决方案2】:

    根据我的经验,这是行不通的:

      public partial class Form1 : Form
      {
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void Form1Load(object sender, EventArgs e)
        {
          Application.ThreadException += ApplicationThreadException;
          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
          AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
        }
    
        void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
        {
          //throw new NotImplementedException();
        }
    
        void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
          //throw new NotImplementedException();
        }
      }
    

    但这确实:

    [STAThread]
    static void Main()
    {
    
      Application.ThreadException += ApplicationThreadException;
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
      AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
    
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }
    
    static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
      //throw new NotImplementedException();
    }
    
    static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
      //throw new NotImplementedException();
    }
    

    也许有必要在 Application.Run() 之前设置全局处理程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-01
      • 2012-09-17
      • 2020-11-26
      • 2015-11-08
      • 2018-07-09
      • 2016-04-18
      • 2014-01-29
      • 2013-01-11
      相关资源
      最近更新 更多