5.15. Breaking on a First Chance Exception


You need to fix a problem with your code that is throwing an exception. Unfortunately, an exception handler is handling the exception, and you are having a tough time pinpointing where and when the exception is being thrown.

Forcing the application to break on an exception before the application has a chance to handle it is very useful in situations where you need to step through the code at the point where the exception is first being thrown. If this exception was thrown and not handled by your application, the debugger would intervene and break on the line of code that caused the unhandled exception. In this case, you can see the context in which the exception was thrown. However, if an exception handler is active when the exception is thrown, the exception handler will handle it and continue on, preventing you from being able to see the context at the point where the exception was thrown. This is the default behavior for all exceptions.


Select Debug Exceptions within Visual Studio .NET to display the Exceptions dialog box (see Figure 5-1). Select the exception from the tree that you want to modify and then click on the “Break into the debugger” radio button in the “When the exception is thrown” frame. Click the OK button and then run your application. Any time the application throws the type of error you selected in the Exceptions dialog box, the debugger will break on that line of code before your application has a ...

Get C# Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.