4

I have code that frequently throws an InvalidOperationException. In this specific case, the exception is actually OK and I don't want the debugger to break when it gets thrown. However, I can't disable all InvalidOperationException breaks because that's just a bad idea.

Supposedly the DebuggerStepThrough or DebuggerHidden attributes are what I need, but the exception bubbles up and ignores the try block - the debugger breaks anyways.

internal class Program
{
    static void Main(string[] args)
    {
        // The debugger breaks at this line.
        // Ideally it should continue execution.
        var x = TestFunc();
    }

    [System.Diagnostics.DebuggerHidden]
    private static bool TestFunc()
    {
        try
        {
            // The actual code is third-party; I can't control the exception.
            throw new InvalidOperationException();
        }
        catch (InvalidOperationException)
        {
            return false;
        }
        catch
        {
            throw;
        }
    }
}

Related issues:

How to NOT breaking on an exception? (This doesn't work because there is no way to pinpoint where the exception has come from. The stack trace only points to the third party code, not my calling code.)

Don't stop debugger at THAT exception when it's thrown and caught (This results in the aforementioned bubbling and circumvention.)

Using VS 2022

  • 4
    Not an answer to your question, but if you're only trying to ignore `InvalidOperationException` exceptions, then that should be specified as: `catch (InvalidOperationException)` – Rufus L Mar 03 '23 at 18:14
  • Have you tried the answer to the first related issue you linked? That seems very promising. – Rufus L Mar 03 '23 at 18:20
  • @RufusL I have. Unfortunately, there is no way to see if the calling method has a `System.Diagnostics.DebuggerHidden` attribute or similar for handling. The exception only has a stack trace from the third party code. – Carl Schmidt Mar 03 '23 at 18:35

2 Answers2

1

You need to go to Tools > Options > Debugging > General > select "Only enable my code (managed only)".

Then use the DebuggerNonUserCodeAttribute to tell the debugger that the function is not part of the code.

[System.Diagnostics.DebuggerNonUserCode()]
private static bool TestFunc()
{
    try
    {
        throw new InvalidOperationException();
    }
    catch (InvalidOperationException)
    {
        return false;
    }
    catch
    {
        throw;
    }
}

If it still doesn't work please let me know.

wenbingeng-MSFT
  • 1,546
  • 1
  • 1
  • 8
  • This has the same effect as the `DebuggerHidden` attribute; Visual Studio breaks on the line calling `TestFunc()`. – Carl Schmidt Mar 06 '23 at 16:15
  • @CarlSchmidt Hi, both the sample code you provided and the code I provided to you, it doesn't break when I test it with the console. So my guess is that there is something wrong with your exception settings. Please follow the steps below: Debug=>Windows=>Exception Settings. Right-click all the options below and click Restore Defaults. – wenbingeng-MSFT Mar 07 '23 at 01:51
0

It doesn't appear to be possible in Visual Studio. You either tell it to break on the exception (Debug -> Windows -> Exception Settings, check the exception) or ignore it. I can live with clicking the Continue button (or F5) a couple of times.

  • ... huh? You've misread my answer entirely. The answer is simply that what I want is not currently possible. Yes the comments helped me reach that conclusion but no, that conclusion is not in anybody's comments. That's why I made the answer. Woe is me for putting it plainly for everyone to see. – Carl Schmidt Mar 11 '23 at 06:22