Visual Studio C# - 不要因该行的任何异常而中断

Car*_*idt 5 c# debugging exception visual-studio

我的代码经常抛出InvalidOperationException. 在这种特定情况下,异常实际上是可以的,我不希望调试器在抛出异常时中断。但是,我无法禁用所有InvalidOperationException中断,因为这是一个坏主意。

据说DebuggerStepThroughorDebuggerHidden属性是我所需要的,但是异常冒出并忽略该try块 - 调试器无论如何都会中断。

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

相关问题:

如何不因异常而中断? (这不起作用,因为无法查明异常的来源。堆栈跟踪仅指向第三方代码,而不是我的调用代码。)

当抛出和捕获异常时,不要停止调试器 (这会导致上述冒泡和规避。)

使用 VS 2022

Car*_*idt 0

在 Visual Studio 中似乎不可能。您可以告诉它在异常发生时中断(调试 -> Windows -> 异常设置,检查异常)或忽略它。我可以忍受点击“继续”按钮(或 F5)几次。