Don't stop debugger at THAT exception when it's thrown and caught

30,110

Solution 1

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.

Solution 2

DebuggerHidden is your friend!

The common language runtime attaches no semantics to this attribute. It is provided for use by source code debuggers. For example, the Visual Studio 2005 debugger does not stop in a method marked with this attribute and does not allow a breakpoint to be set in the method. Other debugger attributes recognized by the Visual Studio 2005 debugger are the DebuggerNonUserCodeAttribute and the DebuggerStepThroughAttribute.

Tested on VS2010 and works great.

While DebuggerStepThrough seems to also work for some specific debugger versions, DebuggerHidden seems to work for a wider range of situations based on the comments to both answers.

Note that both options do not currently work with iterator block methods or for async/await methods. This could be fixed in a later update of Visual Studio.

Note that it does not work with combination of .NET Core + Rider, you can vote the issue.

Solution 3

DebuggerStepThrough is the one to be used to prevent the debugger to break in a method where there is a try/catch.

But it only works if you didn't uncheck the option "Enable Just My Code (Managed Only)" in the General settings of the Visual Studio's Debugging Options (menu Tools/Options, node Debugging/General)...

More info about that attribute on http://abhijitjana.net/2010/09/22/tips-on-debugging-using-debuggerstepthrough-attribute/

DebuggerHidden will simply prevent the Debugger to display the method where the exception is thrown. Instead, it will show the first method on the stack which is not marked with that attribute...

Solution 4

The attributes specified in the other answers (and other ones such as DebuggerNonUserCode attribute) no longer work in the same way by default in Visual Studio 2015. The debugger will break on exceptions in methods market with those attributes, unlike in older versions of VS. To turn off the performance enhancement which changed their behaviour you need to change a registry setting:

reg add HKCU\Software\Microsoft\VisualStudio\14.0_Config\Debugger\Engine /v AlwaysEnableExceptionCallbacksOutsideMyCode /t REG_DWORD /d 1

More information can be found on the visual studio blog.

(This should probably be a comment on the top answer but I don't have enough rep)

Solution 5

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.

Share:
30,110
MichaelD
Author by

MichaelD

Updated on July 16, 2022

Comments

  • MichaelD
    MichaelD almost 2 years

    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