我正在编写一个小型库,可以捕获所有未处理的异常,并显示一个小对话框(类似于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();
};
它完美地工作了。有没有人知道为什么它不起作用?