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.
-
1The `ExceptionThrown` event cannot help here. At the time it is raised, it is not clear whether the exception (just being thrown) will be caught or not. You might be better off using the [Microsoft.Diagnostics.Runtime](https://github.com/Microsoft/clrmd) (also a NuGet package). – Christian.K Apr 03 '19 at 17:36
2 Answers
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.

- 380
- 2
- 13
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.

- 28,327
- 8
- 59
- 66
-
Thanks for the suggestion. But I don't want to modify my application source code. I'm trying to implement a profiler which can monitor all the unhandled exceptions. Is there a way to achieve it using profiling API? – Zader Apr 02 '19 at 19:04