未处理异常事件不起作用?

9

我正在编写一个小型库,可以捕获所有未处理的异常,并显示一个小对话框(类似于NF的常规对话框),让用户有机会将异常发送给开发者。为了实现这一点,我使用AppDomain的UnhandledException事件,代码如下:

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表达式,尝试调试代码的第一行,然后显示发生的错误,而不会处理lambda的其余部分。 但是,如果我只写:

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

它完美地工作了。有没有人知道为什么它不起作用?


1
将您的顶部代码放在try catch中,并在catch中设置监视点。是否有异常,如果有,是什么? - TheKingDave
那么,仅在测试单元中测试异常处理程序的创建呢?这时会发生什么? - Eyal H
2个回答

5
可能有两个原因。
其中一个是您没有正确设置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()之前设置全局处理程序可能是必要的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接