UnhandledException事件不起作用? [英] UnhandledException Event doesn't work?
问题描述
我正在编写一个小库,其中捕获了所有未处理的异常,并显示了一个小对话框(类似于NF的常规对话框),使用户可以将异常发送给开发人员。为此,我像这样使用AppDomain的UnhandledException-Event:
I'm writing a little library which catches all unhandled exceptions, shows a little dialog (similar to the usual dialog of the NF) which gives the user the opportunity to send the exception to the developer. To do this, I use the UnhandledException-Event of the AppDomain like this:
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。
但是,如果我只写:
ExceptionHandler and ExEntry are Classes of my Library. But: If an Exception occurs, the compiler jumps into my Lambda-Expression, tries to debug the first line of code and then shows the error which occurred before without working off the rest of the lambda. But if I just write:
app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
{
ExceptionDialog exdialog = new ExceptionDialog(handler);
exdialog.ShowDialog();
};
它完美地工作。有谁知道为什么这行不通?
it works perfectly. Has anyone an idea why this doesn't work?
推荐答案
可能有两个原因。
One is you did not set UnhandledExceptionMode properly:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
另一个是您没有处理 ThreadException ,并且抛出的异常不是未处理的异常,而是线程异常。
The other is you did not handle the ThreadException, and the thrown exception was not an unhandled exception but a thread exception.
下面是一个示例,您需要根据自己的情况进行修改:
The following is an example, you would need to modify it according to your scenario:
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());
这篇关于UnhandledException事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!