.NET - First chance exception listener for intensive debugging?
Asked Answered
C

2

8

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 :)

Chengteh answered 4/6, 2009 at 18:31 Comment(0)
C
3

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);
        };
Carol answered 20/2, 2013 at 11:23 Comment(1)
Very nice find! Thanks much for dusting off this old question!Chengteh
S
7

You can use the .net profiling API to get notifications of exceptions at all sorts of states, these are the available methods:

ExceptionThrown
ExceptionSearchFunctionEnter
ExceptionSearchFunctionLeave
ExceptionSearchFilterEnter
ExceptionSearchFilterLeave
ExceptionSearchCatcherFound
ExceptionOSHandlerEnter
ExceptionOSHandlerLeave
ExceptionUnwindFunctionEnter
ExceptionUnwindFunctionLeave
ExceptionUnwindFinallyEnter
ExceptionUnwindFinallyLeave
ExceptionCatcherEnter
ExceptionCatcherLeave
ExceptionCLRCatcherFound
ExceptionCLRCatcherExecute

Using the profiling api is not entirely for the faint of heart; have a look at http://msdn.microsoft.com/en-us/library/ms404386.aspx as an entry point for your research and http://msdn.microsoft.com/en-us/library/bb384687.aspx for exception handling specifically.

I'm not aware of a simple way to do it within your managed code such as

AppDomain.FirstChanceException += new EventHandler...

event or similar.

EDIT: A possibly better alternative is using the unamanaged debugging API instead.

Basically you can set a ICorManagedCallback/ICorManagedCallback2 callback using ICorDebug::SetManagedHandler and get callbacks when exceptions occur.

I'm not experienced enough in this area to know what the advantages/disadvantages are over the profiling api.

I just had a look at the mdgb sample which uses the ICorDebug APIs and it seems to get quite enough notifications from exceptions (to quickly see what events occur, set a breakpoint in the HandleEvent method in corapi/Debugger.cs:406)

Selfconsistent answered 4/6, 2009 at 18:42 Comment(2)
I didn't expect it to be easy, but an in-road is all I need to at least get a feel for it. Thanks much!Chengteh
I edited my answer as after playing around with mdbg, I believe possibly the ICorDebug API might be a better way to do it.Selfconsistent
C
3

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);
        };
Carol answered 20/2, 2013 at 11:23 Comment(1)
Very nice find! Thanks much for dusting off this old question!Chengteh

© 2022 - 2024 — McMap. All rights reserved.