Using First Chance Exceptions to Find the Problems
First chance exceptions in the debugger are useful because they allow the debugger to process an exception before the code does. So when running the code above with first chance exceptions enabled, the debugger would see the exception prior to the execution of the catch block. This makes the exception instantly visible so you can make a decision as to whether the exception is avoidable or not. Eliminating unnecessary exceptions can dramatically improve overall code performance.
Debugging with First Chance Exceptions
Enabling first chance exceptions in Visual Studio is simple. The process is slightly different for Visual Studio 2003 and Visual Studio 2005, though neither is complex.
Visual Studio 2003
- From the Debug menu, select Exceptions (or press Ctrl-Alt E). The dialog shown in Figure 1 appears.
- Select the first major grouping, in this case C++ Exceptions, in the list.
- In the When the exception is thrown group select the Break into the debugger radio button.
- Select the next item in the Exceptions list
- Repeat steps 3 and 4 as necessary to select all of the items in the list.
- Click the OK button
Visual Studio 2005
Figure 1. In Visual Studio 2003: This dialog box in Visual Studio 2003 lets you enable first chance exceptions.
Figure 2. In Visual Studio 2005: This dialog box in Visual Studio 2005 lets you enable first chance exceptions.
- From the Debug menu, select Exceptions (or press Ctrl-D, E). The dialog shown in Figure 2 appears.
- Check all of the check boxes in the "Thrown" column.
- Click the OK button.
Once you've enabled first chance exceptions, run your application in the debugger by pressing F5. When an exception occurs you'll be prompted to take action before the code has a chance to respond. Figure 3
shows the dialog that Visual Studio 2003 raises and Figure 4
shows the popup that Visual Studio 2005 raises.
Figure 3. In Visual Studio 2003: The screen shot shows a first chance exception warning generated from the debugger.
Figure 4. In Visual Studio 2005: The screen shot shows a first chance exception warning generated from the debugger.
The VS2003 dialog is very plain but it does allow you to Break
into the debugger and examine the situation, including the call stack, variables, etc., as well as a Continue
option that hands exception handling over to the code. The Break option is essential; it is the means by which you discover the conditions that caused the exception, thereby allowing you to avoid it.
The VS2005 popup is substantially more friendly. It includes a list of potential solutions for the exception in the Troubleshooting tips listbox and provides mechanisms to copy the exception to the clipboardif you want to do more research later. Unlike with VS2003, the 2005 debugger doesn't ask you whether you want to break into the codeit just does. All you have to do is research with the debugging tools and, when ready, hit the Run/Continue key, F5, to resume the application.
Unlike last chance exceptions, first chance exceptions won't automatically end the program if you don't deal with them in the debugger. If code handles the exception then that application will continue running even if you didn't address the exception in the debugger.