How to Stop on First Chance Exceptions - Visual Studio .NET 2003

How to Stop on First Chance Exceptions - Visual Studio .NET 2003

  • Comments 8
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.

  • Yesterday, I talked about how to enable stopping in the debugger on first chance exceptions when using...
  • I typically change all the exceptions to stop in the debugger when thrown. Then I selectively go through and pick which ones to let the machine handle. Plus I end up adding several Win32 exceptions that COM and MSXML throw.

    Then after a few days VS 2003 will loose all my changes and I have to start all over. (Once I notice).

    So my comment/request is, is there a way to set these options via a macro or script or addin? Or some way to export-import these settings?

    And I really wish MS had set the default to break into the debugger on them. Too many are just ignored by programmers because it does not throw up a big message box.
  • Phillip, The changes you make to the debugger's exception handling is on a per project basis. When you save the project, these changes should also be saved. If this same behavior occurs in Visual Studio 2005 beta 2, please report it via the Product Feeback Center (http://lab.msdn.microsoft.com/productfeedback).

    Thanks!
    --DK
  • Since last night (Thursday), my smartphone has, literally, been my conduit to the world; I had to drive...
  • <P>hi i have first-chance exception in my program in both debug and release module. the wield thing is this exception only happen in my machine, if i copy the whole project to other machines, and recompile, there is no such problem!!!!(drive me craze) can u tell me some possibilities about such situation? thanks [Edit: email address removed]</P>
  • Fish, first chance exceptions do not necessarilly indicate a problem in the application. If you configure Visual Studio to stop when the exception is thrown, you may be able to determine why it is occuring on your specific system.
    -- DK
  • A few days ago, I posted on how to configure the debugger to stop on first chance exceptions in Visual...
  • PingBack from http://www.keyongtech.com/716230-first-chance-exception

Page 1 of 1 (8 items)