views:

2837

answers:

4

i would like Visual Studio to break when a handled exception happens (i.e. i don't just want to see a "First chance" message, i want to debug the actual exception).

e.g. i want the debugger to break at the exception:

try
{
   System.IO.File.Delete(someFilename);
}
catch (Exception)
{
   //we really don't care at runtime if the file couldn't be deleted
}

i came across these notes for Visual Studio.NET:

1) In VS.NET go to the Debug Menu >> "Exceptions..." >> "Common Language Runtime Exceptions" >> "System" and select "System.NullReferenceException"

2) In the bottom of that dialog there is a "When the exception is thrown:" group box, select "Break into the debugger"

3) Run your scenario. When the exception is thrown, the debugger will stop and notify you with a dialog that says something like: "An exception of type "System.NullReferenceException" has been thrown. [Break] [Continue]"

Hit [Break]. This will put you on the line of code that's causing the problem.

But they do not apply to Visual Studio 2005 (there is no Exceptions option on the Debug menu.)

Does anyone know where the find this options dialog in Visual Studio that the "When the exception is thrown" group box, with the option to "Break into the debugger"?

Update: The problem was that my Debug menu didn't have an Exceptions item. i customized the menu to manually add it.

+9  A: 

There is an 'exceptions' window in VS2005 ... try Ctrl-Alt-E when debugging and click on the 'Thrown' checkbox for the exception you want to stop on.

Rob Walker
That's exactly what i want. Where is that in the menu? For the life of me i cannot find it.
Ian Boyd
The keyboard shortcut can change according to profile (C# developer, C++ developer, etc.)
Asaf R
I have a menu item: Debug -> Exceptions. This is with VS2005 Professional. Are you running an Express edition?
Rob Walker
if you want the Exceptions menu item: Select Tools | Customize.... Click on the Commands tab. Select the Debug category in the Categories box. Find the Exceptions... command item, then drag it to the Debug menu at the top of the application, dropping it in the appropriate place in the menu.(found in the comments on: http://blogs.vertigo.com/personal/keithc/Blog/archive/2007/07/20/missing-menu-options-in-visual-studio-2005.aspx )
sotto
+8  A: 

With a solution open, go to the Debug - Exceptions (Ctrl+D,E) menu option. From there you can choose to break on Thrown or User-unhandled exceptions.

EDIT: My instance is set up with the C# "profile" perhaps it isn't there for other profiles?

Austin Salonen
i just don't have such an option. That would explain my confusion.
Ian Boyd
Are you missing the Debug menu altogether?
Austin Salonen
i had the debug menu, but no Exceptions options. i customized the menu to manually add it; the shortcut key worked either way (Ctrl+Alt+E)
Ian Boyd
+1  A: 

Check this out on msdn, it explains how to set this up.

Essentially, here are the steps (during debugging):

  1. On the Debug menu, click Exceptions.

  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.

Guy Starbuck
A: 

A technique I use is something like the following. Define a global variable that you can use for one or multiple try catch blocks depending on what you're trying to debug and use the following structure:

if(!GlobalTestingBool)
{
   try
   {
      SomeErrorProneMethod();
   }
   catch (...)
   {
      // ... Error handling ...
   }
}
else
{
   SomeErrorProneMethod();
}

I find this gives me a bit more flexibility in terms of testing because there are still some exceptions I don't want the IDE to break on.

Spencer Ruport
This is a useful technique -- it turns the handled exception into an unhandled exception, so the debugger stops on it automatically. I'm not sure why it was downvoted, unless it was that there's no good way to set your GlobalTestingBool. Another, possibly better option is to use Debugger.IsAttached.
Joe White