New HandleProcessCorruptedStateExceptions attribute in .Net 4

Posted by Yaakov Davis on Stack Overflow See other posts from Stack Overflow or by Yaakov Davis
Published on 2010-06-02T06:27:30Z Indexed on 2010/06/02 6:33 UTC
Read the original article Hit count: 966

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");. (Also managed to crash it using Hans's code from here: http://stackoverflow.com/questions/2950130/how-to-simulate-a-corrupt-state-exception-in-net-4).

The app 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..
        }

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

When running in VS, an exception is thrown, but it doesn't 'bubble' up to those exception handler blocks. When running as standalone (w/o debugger attached), I don't get any log, despite what I expect.

Does anyone has an idea why is it so, and how to make the handling code to be executed?

Many thanks, Yaakov

© Stack Overflow or respective owner

Related posts about .NET

Related posts about exception