New HandleProcessCorruptedStateExceptions attribute in .NET 4

5 .net crash exception-handling exception

I'm trying to crash my WPF application, and capture the exception using the above new .NET 4 attribute.

I managed to manually crash my application by calling Environment.FailFast("crash");. (I also managed to crash it using Hans's code from "How to simulate a corrupt state exception in .NET 4?".)

The application calls the above crashing code when pressing on a button. Here are my exception handlers:

    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..
    }
Run Code Online (Sandbox Code Playgroud)

The //log... comment shown above is just for illustration; there's real logging code there.

在Visual Studio中运行时,会抛出异常,但它不会"冒泡"到这些异常处理程序块.当作为独立运行时(没有附加调试器),我没有得到任何日志,尽管我期望.

为什么会这样,以及如何使处理代码被执行?

jde*_*aan 5

该属性必须放在包含try/catch而不是事件处理程序的方法中.

我在回答这个问题时提供了一个例子

  • 不,这两个AppDomain事件的评论都没有说你可以在事件处理程序上使用它.OP只是缺少必需的`SecurityCritical`属性. (2认同)