5

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

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

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

Why is it so, and how to make the handling code to be executed?

Community
  • 1
  • 1

3 Answers3

5

The attribute has to be put to the method containing a try/catch not to the event handlers.

An example is provided in my answer to this question

Community
  • 1
  • 1
jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • 2
    No it doesn't the remarks for both of the AppDomain events says you can use it on the event handler. The OP is just missing the `SecurityCritical` attribute that is also required. – Scott Chamberlain Jan 26 '17 at 00:30
2
  1. The purpose of FailFast() is to exit immediately, this is why the handlers are not called.
  2. Even some of the 'corrupt state exceptions' cannot be caught by these handlers - an important example is StackOverflowException (I've actually tried to catch one in an ASP.NET app and it did not work, although the attribute was present).

The answer is based on this post: www.naveenbhat.in/2013/02/tips-and-tricks-of-exception-handling_28.html

2

The event handler must be marked with both [HandleProcessCorruptedStateExceptions] and [SecurityCritical] for the event handler to be fired. This requirement is mentioned in the remarks section of FirstChanceException and UnhandledException.

The remarks of DispatcherUnhandledException does not state that you can handle corrupted state exceptions in it so it may not be possible with that event.

Also note in the remarks, it is highly recommended that your FirstChanceException be created inside a Constrained Excution Region to prevent a infinite loop of Stack Overflows or Out of Memory exceptions.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431