我可以以编程方式启用/禁用打破异常吗? [英] Can I enable/disable breaking on Exceptions programmatically?

查看:103
本文介绍了我可以以编程方式启用/禁用打破异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在调试时打破异常……就像在Visual Studio 2008的菜单调试/异常对话框中一样,除了在我要调试的位之前,我的程序有很多有效的异常。

I want to be able to break on Exceptions when debugging... like in Visual Studio 2008's Menu Debug/Exception Dialog, except my program has many valid exceptions before I get to the bit I wish to debug.

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

So instead of manually enabling and disabling it using the dialog every time is it possible to do it automatically with a #pragma or some other method so it only happens in a specific piece of code?

推荐答案

做到这一点的唯一方法是将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()
    {
        ...
    {
}

这篇关于我可以以编程方式启用/禁用打破异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆