How to: Break When an Exception is Thrown
This topic applies to:
Pro, Premium, and Ultimate
The debugger can break execution of your application immediately when an exception occurs, giving you a chance to debug the exception before a handler is invoked.
If you are debugging with How to: Step Into Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code. If the exception is completely unhandled, however, the debugger always breaks.
When you set the debugger to break when a CLR exception is thrown and the debugger breaks on a CLR exception, the line highlighted by the debugger may be slightly off in some cases. This can occur, for example, if you throw an exception from managed code within an if statement. The debugger highlights the line with the next CLR instruction to be executed, which is the line after the throw rather than the line with the throw.
By default, the Exceptions dialog box lists the most common exceptions in each category. You can add your own exceptions and delete exceptions you have added. Visual Studio saves the list of added exceptions with the solution data, so the exceptions will be available the next time you open and run the project.
The Exceptions dialog box provides keys for setting controls without using the mouse. The hot keys are indicated by underlining in the control labels. If you have Windows themes disabled, the underlining will not be visible, although hot keys will still work. To view the hot-key underlining, enable Windows themes using the Windows Control Panel.
To break execution when an exception is thrown
On the Debug menu, click Exceptions.
If the Exceptions command does not appear on the Debug menu, you can add it. For more information, see To add the Exceptions command to the Debug menu.
In the Exceptions dialog box, select Thrown for an entire category of exceptions, for example, Common Language Runtime Exceptions.
Expand the node for a category of exceptions, for example, Common Language Runtime Exceptions, and select Thrown for a specific exception within that category.
To add the Exceptions command to the Debug menu
On the Tools menu, click Customize.
The Customize dialog box appears.
Click the Commands tab and, in the Menu bar list, click Debug.
Click Add Command.
In Categories in the Add Command dialog box, click Debug.
In Commands, click Exceptions and then click OK.
(Optional) You can click Move Down to adjust the position of the Exceptions command on the Debug menu.