Customize Visual Studio's Reaction to Exceptions

Customize Visual Studio's Reaction to Exceptions

Exceptions are a sometimes-frustrating part of debugging and developing. You can configure Visual Studio to deal differently with certain exceptions.

When debugging a program in Visual Studio, a number of situations may cause the debugger to enter break mode. When the debugger enters break mode, program execution is suspended, allowing you—the developer—the opportunity to examine and change the program variables. With Visual Studio 2005, you can even alter the program's underlying source code when in break mode and have the program continue with the edited source.

A common way that break mode is entered is through breakpoints [Hack #36] . Another way that break mode is regularly entered is when an exception is raised that is not handled by your application. Any exception that bubbles up out of your user code will cause the Visual Studio debugger to display information about the exception. While you will likely always want to be notified of an unhandled exception when debugging, you may want to break when an exception is thrown, regardless of whether or not it's handled. Visual Studio can be easily customized to break immediately when a particular type of exception is thrown.

Use the Exceptions Dialog Box

To customize Visual Studio's behavior when encountering exceptions, go to Debug Exceptions or press Ctrl-Alt-E (Debug.Exceptions). This will display the Exceptions dialog, shown in Figure.

Exceptions dialog

The Exceptions dialog allows you to specify Visual Studio's behavior when encountering an exception of a specific type. As discussed earlier, the default behavior is to continue when the exception is thrown and to break into the debugger if the exception is not handled. To modify these settings for a particular exception type, simply choose the exception type from the tree of exceptions and customize the radio buttons, indicating the debugger's behavior.

The Express versions of Visual Studio 2005 do not offer the complete debugging experience provided by the full version of Visual Studio 2005. At the time of this writing, the Express versions allow specifying the exception behavior only for all exceptions, not for specific ones as shown in the Exceptions dialog in Figure.

Understand that changing the setting for a particular exception modifies the behavior for any of those derived exception types whose Use Parent Setting option is selected. By default, all derived exceptions have this Use Parent Setting checked, which means that, by default, changing the behavior of an exception will propagate those changes to its derived exceptions.

Add Custom Exceptions

The Exceptions dialog allows you to change the debugger's behavior when working with system-defined exceptions, but you may want to tailor the behavior of custom exception types you have created. To accomplish this, select the topmost item in the tree of exceptions—Common Language Runtime Exceptions—and click the Add button. This will display a dialog prompting you to enter the name of your custom exception (see Figure). The exception name is case insensitive, but be sure to enter the fully qualified name of your exception, including the namespace.

Enter the name of your custom exception

Once a custom exception has been added to the list, you can remove it by selecting the exception and clicking the Delete button; you can remove all custom exceptions by clicking the Clear All button.

Quickly Finding Exception Names

While the Exceptions dialog lists the Common Runtime Language exceptions in an easy-to-navigate tree, there may be times when you don't know where in the tree the exception resides, or you may know the name of the exception and don't want to have to scroll through the tree interface. Fortunately, the Exceptions dialog offers a Find button that, when clicked, will prompt you for the name, or part of the name, of the exception.

Once you have entered the exception name and click OK, the Exceptions dialog will highlight those exceptions in the tree that match your search. You can click the Find Next button to enumerate through the matches.

Scott Mitchell

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows