Compartir a través de


How to: Break When an Exception is Thrown

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 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.

Note

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.

While Using the Exceptions Dialog Box

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.

Note

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.

For more information, see How to: Add New Exceptions and How to: Delete User-Added Exceptions.

To break execution when an exception is thrown

  1. On the Debug menu, click Exceptions.

    Note

    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.

  2. In the Exceptions dialog box, select Thrown for an entire category of exceptions, for example, Common Language Runtime Exceptions.

    -or-

    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

  1. On the Tools menu, click Customize.

    The Customize dialog box appears.

  2. Click the Commands tab and, in the Menu bar list, click Debug.

  3. Click Add Command.

  4. In Categories in the Add Command dialog box, click Debug.

  5. In Commands, click Exceptions and then click OK.

  6. (Optional) You can click Move Down to adjust the position of the Exceptions command on the Debug menu.

  7. Click Close.

See Also

Tasks

How to: Break on User-Unhandled Exceptions

Concepts

Exception Handling (Debugging)

Just My Code