First-Chance Exception

A first-chance exception is an exception at the beginning of the exception handling process, as observed in the context of debugging a Windows application.

What They Are

When an exception is first thrown, whether from the application itself or from dependent or platform code, it is known a “first chance” exception – the debugger is given the first chance of inspecting the exception prior to the application handling it (or not). All exceptions start as first chance exceptions, and the debugger gets notified each time an exception is thrown.


Note

If you are using Visual Studio as a debugger, you can see these notifications in the Output window and in IntelliTrace. You can configure the debugger to automatically break for specific exceptions, which functions just like setting a breakpoint.


As Windows developers are aware, during debugging, once execution has broken due to a first chance exception, you can continue debugging by stepping or continuing. When you choose to continue, the code has the opportunity to handle the exception. If it does not, then the exception falls into one of the following two categories.

User-unhandled Exceptions

When a first chance exception is not caught (handled) in user code and is caught in “External Code” on the call stack, it is classified as a “user-unhandled” exception. (This classification is only generally applicable when debugging managed code Just My Code enabled. By default, the debugger will break for all user-unhandled exceptions.

Once execution broken due to a user-unhandled exception, you can continue debugging by stepping or pressing Continue. The exception may be handled somewhere in "External Code" on the call stack, and if it isn’t, then it becomes an unhandled exception.

You can change the default, but in most cases you will likely not need to change this setting. Most frameworks, like ASP.NET, implement global exception handlers so that your application will not crash, but the exception has not really been dealt with properly. The debugger provides breaking for user-unhandled exceptions to notify you in these cases.

Unhandled Exceptions

When a first chance exception is not handled by the application, and nothing else catches it, it will eventually reach the system default handler. In this case, the exception is classified as unhandled, and the application process will crash. If you’ve attached a debugger, it will break in this case, allowing you to examine the application state that lead to the crash. Unfortunately, it is not possible to resume execution after an unhandled exception.