gpt4 book ai didi

c# - 我可以以编程方式启用/禁用中断异常吗?

转载 作者:可可西里 更新时间:2023-11-01 09:08:30 25 4
gpt4 key购买 nike

我希望能够在调试时中断异常...就像在 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()
{
...
{
}

关于c# - 我可以以编程方式启用/禁用中断异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2738115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com