Breaking on first chance exceptions

According to MSDN a first chance exception:

Occurs when an exception is thrown in managed code, before the runtime searches the call stack for an exception handler in the application domain.

A first chance exception is not another type of exception like a NullReferenceException. Every exception that is thrown in managed code starts out as a first chance exception. After that happens it may be caught somewhere higher in the call stack or it may not be caught by your code but instead end up on the screen of the user of your software

Exceptions that are not or improperly handled in the call stack may lead to unexpected behavior of your software. Because of this when debugging a difficult problem one of the first things I always do is to configure my IDE (Visual Studio) to break on any exception in my code. This enables me to see things happening when they happen and then I can choose whether I want to ignore that exception in the future. Visual Studio can be configured to break on every exception when they are just first chance exceptions through the Exception Settings (debug > Window > Exception Settings or use CTRL+ALT+E). To enable this tell Visual Studio to “break when thrown” all Common Language Runtime Exceptions:

Now when exceptions are thrown the debugger will break even if they would normally be ignored because they would end up in a catch or using block. You can still choose to ignore the exception on future runs if they are not helpful in your debugging process by removing the “Break when thrown” for this specific exception.

Was this article helpful?