views:

1063

answers:

5

In tools/exceptions, I've set the option that the debugger stops when an exception is thrown. Whether it is caught or not .

How do I exclude an exception of that rule? Somewhere in my code there is a caught exception that is part of the program logic. So I obviously don't want that exception to stop the debugger each time it is hit.

Example: I want to ignore the nullreference exception (which is caught) on line 344 . I want to stop at all other exceptions

+7  A: 

In Visual Studio you can specify exactly which exceptions you want to halt on. Go to Debug > Exceptions and fold out the relevant branch. From here you can check/uncheck the exceptions you want.

Brian Rasmussen
it's a particular place in code, not the type of the exception
MichaelD
a good shortcut Ctrl+Alt+E in VS will bring up the exception dialog
Simon
Not sure I follow you then. Do you want to have the debugger ignore any or specific exceptions for only a part of the code and not the rest?
Brian Rasmussen
Example: I want to ignore the nullreference exception (which is caught) on line 344 . I want to stop at all other exceptions
MichaelD
It is besides the point here, but NullReferenceException is usually a sign of a coding error and chances are that you're doing something wrong by catching it.
Brian Rasmussen
If I understand you correct, you could just uncheck NullReferenceException and leave the rest checked. Wouldn't that accomplish what you're after?
Brian Rasmussen
He wants to ignore only one NullReferenceException at a special location - all other NullReferenceException should still break. In my opinion this is a design problem, because it sounds as if he uses the NullReferenceException for programming logic. He should consider to introduce a new exception type for this.
tanascius
@tenascius: You're probably right. Catching NullReferenceException is rarely a good idea.
Brian Rasmussen
that would result in all nullreference exeptions being ignored.I only want to ignore the nullreference exception on line 344. I know it's not the best architecture but at the moment we don't have time to adjust it and i need my debugger as efficient as possible:)
MichaelD
just check for null before the line of code
Simon
that is due to architectural reasons not possible
MichaelD
here is the problem:http://stackoverflow.com/questions/1957907/how-do-i-know-when-a-lambda-expression-is-null
MichaelD
Adding DebuggerStepThrough attribute to a method in Visual Studio 2010 will prevent the debugger from halting an an unhandled exception is thrown by the method.
Tim Murphy
+1  A: 

You are not able to single out an exception thrown at a specific place in your code. You are however able to disable exeptions of a specific type.

If your own code throws the exception in question, i would make it a custom exception, derived from whatever fits, and then disable debug breaking on this derived type.

Disabling system exeptions as NullReferenceException will affect the entire system, which of course isnt desirable during development.

Note that there is two kinds of break-behaviors for exceptions:

  • Thrown: If selected, breaks as soon as a exception of this type is thrown
  • User-unhandled: If selected, breaks only if the exception, of this type, is not handled by a try/catch.

You could remove the check in 'Thrown' for the NullReferenceException which will give you the benefit of not breaking each time your system passes the line in question in your code, but still breaking if you have some unhandled NullReference expection occuring in other parts of the system.

Lars Udengaard
Adding DebuggerStepThrough attribute to a method in Visual Studio 2010 will prevent the debugger from halting an an unhandled exception is thrown by the method.
Tim Murphy
I tested and it doesn't prevent; it still stops
Shimmy
A: 

If I recall correctly you can use a DebuggerStepThrough attribute on the method that contains the code you don't want exception to fire. I suppose you can isolate the code that fires the annoying exception in a method and decorate it with the attribute.

HTH, Chris

Chris Chou
great! thx for the info
MichaelD
From malinger's answer, and my experience, this answer appears to be incorrect. The `DebuggerStepThrough` attribute does not affect the debugger's behavior with first-chance exceptions.
Michael Petrotta
Yes you are right. I just tried it. I second tanascius on that it does seem a design issue. You can check if something is null first without tripping exceptions.
Chris Chou
+1 Adding DebuggerStepThrough attribute to a method in Visual Studio 2010 will prevent the debugger from halting an an unhandled exception is thrown by the method.
Tim Murphy
@Tim, I tested and it does NOT halt. checkout my answer: http://stackoverflow.com/questions/1420390/3455100#3455100
Shimmy
+2  A: 

That don't work Chris. Tested with VS2008. DebuggerStepThrough works only when stepping into method/class, exception catching ignore them.

maliger
Exactly. So marked answer it incorrect.
Alex Yakunin
Adding DebuggerStepThrough attribute to a method in Visual Studio 2010 will prevent the debugger from halting an an unhandled exception is thrown by the method.
Tim Murphy
+1  A: 

DebuggerHidden is your friend!

Tested on VS2010 and works great.

Shimmy
working on VS2008. You have to apply it to the whole method including the catch block, or you'll just break somewhere else
Mark Heath