Monday, January 11, 2010

How to Enable First Chance Exceptions in Visual Studio

This blog entry explains how to enable first chance exceptions in Visual Studio. I explained this several people, so this might be a basic configuration but there are many people who apparently do not yet know.

First chance exceptions means that Visual Studio immediately stops when any kind of exception (e.g. NullReferenceException or InvalidOperationException) occurs. Often first chance exceptions are disabled in Visual Studio by default installation. This causes exceptions that can be caught by a catch-block, wherever in call stack, will not stop the debugger at the point of exception creation. The debugger only stops if an exception cannot be handled by any catch-block. So a developer often does not see where the exception was created and thrown.

Depending on your installation of Visual Studio you first have to add an additional command to the menu of your IDE. Therefore select "Tools" menu and click "Customize...".


The "Customize" dialog opens where you can select additional items to be added to your menu or toolbars. Activate the "Commands" tab and select "Debug" in the left list with title "Categories". Select "Exceptions..." item from the right list with title "Commands" and drag it into your "Debug" menu on top of Visual Studio.


Now you can close the Customize dialog by hitting the "Close" button.

Go to menu and select the newly added menu item "Exceptions..." in "Debug" top menu. The new "Exceptions" dialog appears where you can configure the exception behavior of Visual Studio debug sessions. Within the shown list select "Common Language Runtime Exceptions" and activate the "Thrown" check-box on the right side.

As you noticed, you also can expand each exception category to select any special kind of exception you want to enable or disable for first chance exception handling. This is useful for some special debugging sessions, but most times its best to force Visual Studio to stop at any kind of thrown exception.

To close the dialog use the "Ok" button.

Now you can start debugging your project and Visual Studio stops as soon as any kind of exception occurs.


Happy coding :-)

5 comments:

  1. :-) thank you !. i have the first-change dialog instead of this one !!!

    ReplyDelete
  2. Many thanks, Florian - my code ran fine in the debugger but not 'for real'; your tip made VS2010 show me where it was failing.

    ReplyDelete
    Replies
    1. Hi Austin
      Thanks for your feedback. You are very welcome!
      - Flo

      Delete
  3. Bless you!!! I have searched for quite a few hours as to why a DataSourceObject Ondeleted property was not causing the event to trigger.

    ReplyDelete
    Replies
    1. Glad that I could help a bit!
      Happy debugging :o)

      Delete