Skip to main content
 首页 » 编程设计

c#之Visual Studio : tell in part of the code to don't exception-break when debugging

2024年02月13日20jillzhang

我正在调试我的代码中的异常,我的调试器设置为在异常出现时中断。

我希望我的调试 session 在我的代码的某个部分而不是另一部分发生异常中断,你知道吗,我可以在我的代码中写一个参数(或其他)来告诉调试器不要这部分代码的异常中断?

我调试的异常都是同类型的API异常,无法按类型过滤。

谢谢

ps:请注意,我知道“调试/异常”,但这对我来说没有用,因为我不想过滤某种类型的异常,只在代码的一部分中过滤它们。

示例:

#region don't want to break at exception 
Try 
{ 
  //I don't want the debugger to break here 
  ApiMethodThatThrowException(); 
} 
Catch(Exception){} 
#endregion 
#region want to break at exception 
Try 
{ 
  //I want the debugger to break here 
  ApiMethodThatThrowException(); 
} 
Catch(Exception){} 
#endregion 

请您参考如下方法:

除了@abelenky 的回答之外,我还想指出,Visual Studio 不允许您禁用某些Exceptions(C++ Exceptions,GPU 内存访问异常 等)。然后,您必须考虑使用 System.Diagnostics 属性在调试器中绕过这些 Exceptions。

DebuggerHiddenAttribute和 DebuggerStepThroughAttribute是可用于告诉调试器跳过某些代码段的两个属性。

public string ConnectionString{ 
    [DebuggerStepThroughAttribute()] 
    get {  
        // Implementation here;  
    } 
} 

以上示例取自: Using Attributes to Improve the Quality..

[DebuggerHiddenAttribute] 
static void Main(string[] args) { 
    // Implementation here; 
}