RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


.NET Offers a "First Chance" to Squelch Performance-killing Hidden Exceptions : Page 2

Uncaught exceptions in your .NET applications can turn an otherwise high-performance application into a snail, especially those that are allowed to be "eaten" by subsequent code. Find out how to use very handy "First Chance" exception feature in the .NET debugger to root out nasty hidden exceptions.

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

  1. From the Debug menu, select Exceptions (or press Ctrl-Alt E). The dialog shown in Figure 1 appears.
  2. Select the first major grouping, in this case C++ Exceptions, in the list.
  3. In the When the exception is thrown group select the Break into the debugger radio button.
  4. Select the next item in the Exceptions list
  5. Repeat steps 3 and 4 as necessary to select all of the items in the list.
  6. Click the OK button

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.

Visual Studio 2005
  1. From the Debug menu, select Exceptions (or press Ctrl-D, E). The dialog shown in Figure 2 appears.
  2. Check all of the check boxes in the "Thrown" column.
  3. 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 clipboard—if you want to do more research later. Unlike with VS2003, the 2005 debugger doesn't ask you whether you want to break into the code—it 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.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date