【发布时间】:2010-06-02 06:27:30
【问题描述】:
我正在尝试使我的WPF 应用程序崩溃,并使用上述新的 .NET 4 属性捕获异常。
我设法通过调用Environment.FailFast("crash"); 手动使我的应用程序崩溃。
(我还设法使用来自“How to simulate a corrupt state exception in .NET 4?”的 Hans 的代码使其崩溃。)
当按下按钮时,应用程序会调用上述崩溃代码。 这是我的异常处理程序:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
DispatcherUnhandledException += app_DispatcherUnhandledException;
}
[HandleProcessCorruptedStateExceptions]
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//log..
}
[HandleProcessCorruptedStateExceptions]
void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
//log..
}
[HandleProcessCorruptedStateExceptions]
void app_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//log..
}
上面显示的//log... 评论仅用于说明;那里有真正的日志记录代码。
在 Visual Studio 中运行时,会引发异常,但不会“冒泡”到那些异常处理程序块。 当独立运行时(没有附加调试器),我没有得到任何日志,尽管我期望。
为什么会这样,如何让处理代码被执行?
【问题讨论】:
-
也许您还需要将 [SecurityCritical] 添加到您的处理程序中。
标签: .net exception exception-handling crash