C# – Catch All (handled or unhandled) Exceptions

cexceptionexception handling

I want to catch all exceptions raised (handled or unhandled) to log them. for unhandled i use ThreadExceptionEventHandler and UnhandledExceptionEventHandler but i want to catch and exceptions that are in try catch block with or without (Exception e). It's possible to inherit the exceptions class to create a general event?

Best Answer

Starting from Windows XP you can get notified about every raised exception, even before it's known to be handled or not. This is available via so-called "vectored" exception handling, which is actually a little addition to the standard structured exception handling.

Call AddVectoredExceptionHandler to add your own handler. You'll get called right after the exception is raised, either by explicit throw/__CxxThrowException/RaiseException or implicitly by the processor (access violation or etc.)

You can log the exception in your handler (produce stack dump for instance), and return EXCEPTION_CONTINUE_SEARCH.