This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

VS 2005+

Visual Studio Break on Exception

When running a program in debug mode, using Visual Studio's debugger, and encountering an exception, the default behaviour is to pause execution unless the error is handled in a try / catch block. This behaviour can be modified for each exception type.

Debug Mode Exceptions

When you execute your software in debug mode, Visual Studio reacts differently to a thrown exception depending upon whether the exception is handled within a try / catch / finally block or is unhandled. When using the default configuration, unhandled exceptions cause the program to halt and the exception details to be displayed. Handled exceptions do not cause the program to stop.

You can see this by executing the following code in debug mode. Although a DivideByZeroException is thrown when the division is attempted, the program continues. If you comment out the try and catch, the exception halts execution.

    int i = 0;
    int j = 1;
    Console.WriteLine(j / i);
    Console.WriteLine("Caught an exception");

This behaviour is useful in most circumstances. However, sometimes you will want to ignore a specific type of exception, even when unhandled, or break on a handled exception that would normally be ignored. You can control this using the Exceptions dialog box, which may be viewed by selecting "Exceptions" from the Debug menu or by pressing Ctrl-Alt-E.

Visual Studio Exceptions Dialog

The main area of the dialog box shows a list of exception types in a tree structure. The branches of the tree can be expanded to show various groups of exceptions and the individual types within each category. Two checkboxes are shown for each exception and group. If the "Thrown" checkbox is ticked, the program will break when the selected exception, or one of the selected group of exceptions, is encountered. This includes exceptions that have been handled. If the "User-unhandled" checkbox is ticked the program will break only if the exception is unhandled.

To try the options, find the DivideByZeroException type in the tree structure. To find an exception quickly, click the Find button and enter part of the name of the item you are searching for. The first matching item will be found. If this is not the desired exception, click the Find Next button to cycle through the matches. Once you have found DivideByZeroException, check the appropriate "Thrown" option. If you run the sample program you will see that the program halts on the handled exception.

Reset All

There are some further options in the Exceptions dialog box that are worthy of note. The first of these is the Reset All button. If you have changed the options in the dialog box to help debug your application, you can reset all of the options to their original settings by clicking this button.

Configuring Custom Exceptions

If you have defined your own exception type that inherits functionality from the Exception class or one of its subclasses, you may wish to configure it using the dialog box. As it will not be a standard exception type, it will not appear in the list by default. However, you can add it by clicking the Add button and providing the details. For .NET exceptions you should ensure that you choose the "Common Language Runtime Exceptions" option from the drop-down list. You should then provide the fully qualified name of the exception class. For example, "MyNamespace.MyException".

30 August 2010