.netdebuggingexceptionfirst-chance-exception

.NET - First chance exception listener for intensive debugging?


This is probably unrealistic, but would it be possible to enable a component to be notified of all first chance exceptions occuring in its process?

We have some third-party (contracted by us) components which fail to do anything but eat excepitions and the politics of the business relationship make the whole ordeal a royal pain.

We also are aware that some of our code is performing the disappointing action of letting exceptions vanish into the abyss rather than using our centralized exception logger.

I assume our application would have to be started as a child process of a debugging application to achieve the effect, but I figure it's worth asking :)


Solution

  • Net 4.0 has actually added the AppDomain.FirstChanceException event. It fires before any catch block is executed.

    This MSDN article has some examples.

    Basically you just add an event handler like this:

        AppDomain.CurrentDomain.FirstChanceException += 
            (object source, FirstChanceExceptionEventArgs e) =>
            {
                Console.WriteLine("FirstChanceException event raised in {0}: {1}",
                    AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
            };