Last week, I talked about what first chance exceptions are and mentioned that you could configure the Visual Studio debugger to allow you to debug them.

Using Visual Studio .NET 2003, you can configure how the debugger handles first chance exceptions on a per exception basis or for exception families (by namespace and/or runtime). 

Here's how you enable a specific exception to be caught.  I will use System.ApplicationException in this example.

  • Start Visual Studio .NET 2003 and load (or create) a project
  • On the Debug menu, select Exceptions (or use Ctrl+Alt+E from the keyboard)
  • Expand the Common Lanugage Runtime Exceptions node in the list
  • Expand the System node
  • Select System.ApplicationException in the list
  • In the area labeled When the exception is thrown (below the list), select Break into the debugger
  • Click OK

Typically, the default value for "when the exception is thrown" (aka first chance exceptions) is "Continue".

To enable an exception family is just as easy.  This example will enable stopping on first chance exceptions for exceptions defined in the System.Net namespace.

  • Start Visual Studio .NET 2003 and load (or create) a project
  • On the Debug menu, select Exceptions (or use Ctrl+Alt+E from the keyboard)
  • Expand the Common Lanugage Runtime Exceptions node in the list
  • Select System.Net in the list
  • In the area labeled When the exception is thrown (below the list), select Break into the debugger
  • Click OK

Note: When enabling for an exception family, specific exception settings override the family level setting.  If you are not catching the desired exception, check the configuration for the exception using the first set of steps above.  Change the option to "Use parent setting" and the specific exception will inherit the family setting.

As mentioned in last week's post, if your code does not handle the exception, the debugger will typically stop with an unhandled exception and allow you to debug where you failed to catch.  Using today's tip, you can debug at the point of the throw and examine the conditions which caused the exceptional situation.

Enjoy!
-- DK

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.