加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我可以通过编程方式启用/禁用异常?

发布时间:2020-12-15 06:46:46 所属栏目:百科 来源:网络整理
导读:我想要在调试时能够打破异常,就像在Visual Studio 2008的菜单调试/异常对话框中一样,除了我的程序有很多有效的异常之后,我想要调试的位. 因此,每次使用对话框手动启用和禁用它都可以使用#pragma或其他方法自动执行,因此只能在特定的代码段中发生. 解决方法
我想要在调试时能够打破异常,就像在Visual Studio 2008的菜单调试/异常对话框中一样,除了我的程序有很多有效的异常之后,我想要调试的位.

因此,每次使用对话框手动启用和禁用它都可以使用#pragma或其他方法自动执行,因此只能在特定的代码段中发生.

解决方法

通过将DebuggerNonUserCodeAttribute放在你的方法上,做这个事情的唯一方法就是这样做.

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

很好的解释它here …

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()
    {
        ...
    {
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读