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.

Community
  • 1
  • 1
MrFox
  • 4,852
  • 7
  • 45
  • 81

3 Answers3

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.

slugster
  • 49,403
  • 14
  • 95
  • 145
  • 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 01 '13 at 09: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 01 '13 at 09:58
  • 1
    Oke I see now the "user unhandled" box is still checked, so normally it should break on this exception. – MrFox Nov 01 '13 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

Sam
  • 7,245
  • 3
  • 25
  • 37
  • The debugger already breaks on the exception, my problem is that in this specific case it should not. – MrFox Nov 01 '13 at 09: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.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • 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 '23 at 17:04