我可以启用异常编程/禁用破?异常

2023-09-03 11:16:48 作者:生死悠关

我希望能够在异常调试时打破......就像在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或其他方法做它,所以它只会发生在一段特定的code?

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?

推荐答案

做一些接近这一点的唯一方法是通过将DebuggerNonUser codeAttribute在你的方法。

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'。是不是我的code!。轻信调试器会相信你,不会在该方法突破:使用属性,使调试器跳过方法完全,甚至当你单步调试code;发生的,然后被抓的方法中不会中断到调试例外。它将把它当作好像它是一个调用框架组件,并应例外走未处理的,将报告上一级调用堆栈,在code,它调用的方法。

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.

code例如:

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