【发布时间】: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 中放置一个观察点。有没有例外,如果有,那是什么?
-
在测试单元中简单地测试异常处理程序的创建怎么样?会发生什么?