英文:
Visual Studio C# - Don't break for any exception at this line
问题
以下是您要翻译的内容:
我有一个经常引发“InvalidOperationException”的代码。在这种特定情况下,异常实际上是可以接受的,我不希望调试器在引发异常时中断。但是,我不能禁用所有“InvalidOperationException”中断,因为那只是一个坏主意。
据说“DebuggerStepThrough”或“DebuggerHidden”属性是我需要的,但异常会冒泡并忽略“try”块 - 调试器仍然会中断。
internal class Program
{
static void Main(string[] args)
{
// 调试器在此处中断。
// 理想情况下,它应该继续执行。
var x = TestFunc();
}
[System.Diagnostics.DebuggerHidden]
private static bool TestFunc()
{
try
{
// 实际的代码是第三方的;我无法控制异常。
throw new InvalidOperationException();
}
catch (InvalidOperationException)
{
return false;
}
catch
{
throw;
}
}
}
相关问题:
https://stackoverflow.com/questions/15833498/how-to-not-breaking-on-an-exception
(这不起作用,因为没有办法确定异常来自何处。堆栈跟踪只指向第三方代码,而不是我的调用代码。)
https://stackoverflow.com/questions/1420390/dont-stop-debugger-at-that-exception-when-its-thrown-and-caught
(这导致了前述的异常传播和规避。)
使用VS 2022
英文:
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:
https://stackoverflow.com/questions/15833498/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.)
https://stackoverflow.com/questions/1420390/dont-stop-debugger-at-that-exception-when-its-thrown-and-caught
(This results in the aforementioned bubbling and circumvention.)
Using VS 2022
答案1
得分: 1
你需要前往"工具" > "选项" > "调试" > "常规" > 选择"仅启用我的代码(仅托管代码)"。
然后,使用DebuggerNonUserCodeAttribute来告诉调试器该函数不是代码的一部分。
[System.Diagnostics.DebuggerNonUserCode()]
private static bool TestFunc()
{
try
{
throw new InvalidOperationException();
}
catch (InvalidOperationException)
{
return false;
}
catch
{
throw;
}
}
如果仍然无法正常工作,请告诉我。
英文:
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.
答案2
得分: 0
这似乎在Visual Studio中是不可能的。您要么让它在异常发生时中断(调试 -> 窗口 -> 异常设置,勾选异常),要么忽略它。我可以接受点击"继续"按钮(或按F5)几次。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论