抛出异常时如何防止调试器进入我的方法?

How can I prevent the debugger from entering my method when Exception is thrown?

如何在抛出异常时阻止调试器进入我的方法,而是在方法的调用位置显示异常?

例如,如果某人的代码导致从 mscorlib 抛出异常,调试器(显然)不会将它们带入非用户代码以显示异常的来源,仅在调用站点显示异常。

换句话说,这是默认行为:

这是我想要的行为:

我尝试将 [DebuggerNonUserCode][DebuggerStepThrough] 属性添加到我的 Fail() 方法,但没有成功。

您需要使用 DebuggerHiddenAttribute:

修饰您的方法
[DebuggerHidden]
public static void Fail() {
    throw new Exception("Fail");
}