UnhandledException事件不起作用?

时间:2013-05-23 15:23:51

标签: c# exception unhandled

我正在编写一个捕获所有未处理异常的小库,显示一个小对话框(类似于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();
        };

它完美无缺。有谁知道为什么这不起作用?

2 个答案:

答案 0 :(得分:3)

可能有两个原因。

一种可能是你没有正确设置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());

答案 1 :(得分:1)

根据我的经验,这不起作用:

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

之前设置全局处理程序可能是必要的
相关问题