ICorProfilerCallback2 : Unable to differentiate handled and unhandled exception from exception events

86 Views Asked by At

I'm using ICorProfilerCallback2 interface to implement a profiler to monitor all the unhandled/uncaught exceptions occurs in any .net application. The ExceptionThrown event helps me to find all the exceptions(caughed/uncaughed exceptions) occured in the program. But I need to capture only unhandled exceptions.

2

There are 2 best solutions below

1
500 - Internal Server Error On

You don't need to use the profiling API for that. Besides, that profiling event fires when the exception is first thrown. It is not known at that time whether or not it will be handled.

To monitor for unhandled exceptions, install a handler in AppDomain.CurrentDomain.UnhandledException.

0
Egozy On

In addition to ExceptionThrown event, which is thrown when the exception is thrown, there are more related events - mostly ExceptionCatcherEnter/ExceptionCatcherLeave, ExceptionSearchFunction* and ExceptionUnwind*. They are thrown when the relevant event occurs. If ExceptionCatcher* event is triggering, then the exception is caught. If the exception is completely uncaught, the either the thread or the program will either terminate. In these cases, you can use ThreadDestroyed and Shutdown methods. You will need to find a way to store the current information during ExceptionThrown (a mapping between ThreadId and current exception status/data, be careful not to store anything that will be invalidated), and only process the information on one of the later callbacks.