1

Normally I want the debugger to break on ArgumentOutOfRangeException.

But within my try catch(ArgumentOutOfRangeException) that exception is already handled and so I want the debugger to not break.

I tried the DebuggerStepThrough attribute, but it still breaks.

9
  • 1
    Remove the try catch and the debugger will break on the right line Nov 1, 2013 at 9:42
  • @ErikKronberg The debugger breaks on the right line, I want it to not break.
    – MrFox
    Nov 1, 2013 at 9:45
  • 1
    What is so wrong with this question that EVERYONE understands the opposite of what I want?
    – MrFox
    Nov 1, 2013 at 9:53
  • because of the title of your question. @MrFox NB. +1 you changed it.
    – Sam
    Nov 1, 2013 at 9:54
  • 1
    @MrFox I understand exactly what you're asking for. Maybe you're not understanding the answer?
    – slugster
    Nov 1, 2013 at 10:04

3 Answers 3

5

You can do this by setting the debugger to break on user unhandled exceptions.

Go to Debug -> Exceptions, Common Language Runtime Exceptions, de-tick (uncheck) the Thrown box. Of course you can get very precise with what you want to break on by drilling down into that list. Be aware that this setting takes effect across the whole solution, you can't set it per class or method. If you do want to be more selective per method, then consider using compile directives to not include that bit of code during debug time.

As for the DebuggerStepThrough attribute, that is to prevent breaking on break points, nothing to do with breaking on exceptions.

3
  • I am aware of this option, note the phrase 'normally' I want the debugger to break here. But in this specific scenario I want it to NOT break.
    – MrFox
    Nov 1, 2013 at 9:52
  • @MrFox This should work for you then - it will only break if the exception is not handled, which in your case it is. Otherwise you should resort to using compilation directives. Like I said, this affects the whole solution, you can't be super selective with it.
    – slugster
    Nov 1, 2013 at 9:58
  • 1
    Oke I see now the "user unhandled" box is still checked, so normally it should break on this exception.
    – MrFox
    Nov 1, 2013 at 11:39
1

You should check your visual studio isn't set to break on all exceptions

On the Debug menu, click Exceptions.

In the Exceptions dialog box, select Thrown for an entire category of exceptions,
for example, Common Language Runtime Exceptions.

Microsoft Visual Studio Help

1
  • The debugger already breaks on the exception, my problem is that in this specific case it should not.
    – MrFox
    Nov 1, 2013 at 9:54
1

There is a way. First disable debugging code that is not yours. Go to Tools > Options > Debugging > General > select "Enable Just My Code (Managed only)". Now tell the debugger that this one function is not part of your code with DebuggerNonUserCodeAttribute:

    [System.Diagnostics.DebuggerNonUserCode()]
    private void FunctionThatCatchesThrownException()
    {
        try
        {
            throw new ArgumentOutOfRangeException();
        }
        catch (ArgumentOutOfRangeException ex)
        {
            //...
        }
    }

If an exception (some other than ArgumentOutOfRangeException) gets out of the function debugger will catch it as usual, but the location of interception will be where the function is called.

1
  • This did not work for me in .NET 6.0 VS2022. I think it somehow works differently between .NET (Core) and .NET Framework
    – RobSiklos
    Jan 13, 2023 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.