且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以以编程方式启用/禁用打破异常吗?

更新时间:2023-02-13 10:00:28

做到这一点的唯一方法是将DebuggerNonUserCodeAttribute放在您的方法上。

The only way to do something close to this is by putting the DebuggerNonUserCodeAttribute on your method.

这将确保标记方法中的任何异常都不会导致异常中断。

This will ensure any exceptions in the marked method will not cause a break on exception.

对此的很好解释此处 ...

Good explanation of it here...


这是一个属性,您将其置于告诉调试器与我无关guv'。不是我的代码!的方法中。灵活的调试器会相信您,并且不会破坏该方法:使用属性可使调试器完全跳过该方法,即使您单步执行代码也是如此;发生的异常,然后被方法捕获,不会破坏调试器。它将视为对框架程序集的调用,并且如果未处理异常,则会在调用方法的代码中在调用堆栈的上一级报告它。

This is an attribute that you put against a method to tell the debugger "Nothing to do with me guv'. Ain't my code!". The gullible debugger will believe you, and won't break in that method: using the attribute makes the debugger skip the method altogether, even when you're stepping through code; exceptions that occur, and are then caught within the method won't break into the debugger. It will treat it as if it were a call to a Framework assembly, and should an exception go unhandled, it will be reported one level up the call stack, in the code that called the method.

代码示例:

public class Foo
{
    [DebuggerNonUserCode]
    public void MethodThatThrowsException()
    {
        ...
    {
}