C# 当它被抛出并被捕获时,不要在那个异常处停止调试器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1420390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:20:14  来源:igfitidea点击:

Don't stop debugger at THAT exception when it's thrown and caught

c#.netvisual-studio-2008.net-3.5debugging

提问by MichaelD

In tools/exceptions, I've set the option that the debugger stops when an exception is thrown. Whether it is caught or not .

在工具/异常中,我设置了在抛出异常时调试器停止的选项。不管是不是被抓到了。

How do I exclude an exception of that rule? Somewhere in my code there is a caught exception that is part of the program logic. So I obviously don't want that exception to stop the debugger each time it is hit.

我如何排除该规则的例外?在我的代码中的某个地方有一个捕获的异常,它是程序逻辑的一部分。因此,我显然不希望该异常在每次命中时都停止调试器。

Example: I want to ignore the nullreference exception (which is caught) on line 344 . I want to stop at all other exceptions

示例:我想忽略第 344 行的 nullreference 异常(被捕获)。我想在所有其他例外情况下停止

采纳答案by Chris Chou

If I recall correctly you can use a DebuggerStepThroughattribute on the method that contains the code you don't want exception to fire. I suppose you can isolate the code that fires the annoying exception in a method and decorate it with the attribute.

如果我没记错的话,您可以DebuggerStepThrough在包含不希望异常触发的代码的方法上使用属性。我想你可以隔离在方法中触发恼人异常的代码并用属性装饰它。

回答by Lars Udengaard

You are not able to single out an exception thrown at a specific place in your code. You are however able to disable exeptions of a specific type.

您无法挑出在代码中特定位置抛出的异常。但是,您可以禁用特定类型的例外。

If your own code throws the exception in question, i would make it a custom exception, derived from whatever fits, and then disable debug breaking on this derived type.

如果您自己的代码抛出有问题的异常,我会将其设为自定义异常,从任何适合的类型派生,然后禁用此派生类型的调试中断。

Disabling system exeptions as NullReferenceException will affect the entire system, which of course isnt desirable during development.

禁用系统异常作为 NullReferenceException 会影响整个系统,这在开发过程中当然是不可取的。

Note that there is two kinds of break-behaviors for exceptions:

请注意,异常有两种中断行为:

  • Thrown: If selected, breaks as soon as a exception of this type is thrown
  • User-unhandled: If selected, breaks only if the exception, of this type, is not handled by a try/catch.
  • 抛出:如果选中,则在抛出此类异常时立即中断
  • 用户未处理:如果选中,则仅在此类异常未被 try/catch 处理时中断。

You could remove the check in 'Thrown' for the NullReferenceException which will give you the benefit of not breaking each time your system passes the line in question in your code, but still breaking if you have some unhandled NullReference expection occuring in other parts of the system.

您可以删除 NullReferenceException 的“Thrown”中的检查,这将使您的好处是每次系统通过代码中的相关行时都不会中断,但如果您在代码的其他部分出现一些未处理的 NullReference 预期,仍然会中断系统。

回答by Shimmy Weitzhandler

DebuggerHiddenis your friend!

DebuggerHidden是你的朋友!

The common language runtime attaches no semantics to this attribute. It is provided for use by source code debuggers. For example, the Visual Studio 2005 debugger does not stop in a method marked with this attribute and does not allow a breakpoint to be set in the method. Other debugger attributes recognized by the Visual Studio 2005 debugger are the DebuggerNonUserCodeAttribute and the DebuggerStepThroughAttribute.

公共语言运行时没有为此属性附加任何语义。它提供给源代码调试器使用。例如,Visual Studio 2005 调试器不会在标记有此属性的方法中停止,并且不允许在该方法中设置断点。Visual Studio 2005 调试器识别的其他调试器属性是 DebuggerNonUserCodeAttribute 和 DebuggerStepThroughAttribute。

Tested on VS2010 and works great.

在 VS2010 上测试过,效果很好。

While DebuggerStepThroughseems to also work for some specific debugger versions, DebuggerHiddenseems to work for a wider range of situations based on the comments to both answers.

虽然DebuggerStepThrough似乎也适用于某些特定的调试器版本,但DebuggerHidden根据对这两个答案的评论,似乎适用于更广泛的情况。

Note that both options do not currently work with iterator block methodsor for async/await methods. This could be fixed in a later update of Visual Studio.

请注意,这两个选项当前不适用于迭代器块方法async/await 方法。这可以在 Visual Studio 的后续更新中修复。

回答by Valery Letroye

DebuggerStepThrough is the one to be used to prevent the debugger to break in a method where there is a try/catch.

DebuggerStepThrough 是用于防止调试器在存在 try/catch 的方法中中断的方法。

But it only works if you didn't uncheck the option "Enable Just My Code (Managed Only)" in the General settings of the Visual Studio's Debugging Options (menu Tools/Options, node Debugging/General)...

但它只有在您没有取消选中 Visual Studio 调试选项(菜单工具/选项、节点调试/常规)的常规设置中的“仅启用我的代码(仅限托管)”选项时才有效...

More info about that attribute on http://abhijitjana.net/2010/09/22/tips-on-debugging-using-debuggerstepthrough-attribute/

有关该属性的更多信息,请访问http://abhijitjana.net/2010/09/22/tips-on-debugging-using-debuggerstepthrough-attribute/

DebuggerHidden will simply prevent the Debugger to display the method where the exception is thrown. Instead, it will show the first method on the stack which is not marked with that attribute...

DebuggerHidden 只会阻止调试器显示抛出异常的方法。相反,它将显示堆栈中未标记该属性的第一个方法...

回答by bhh

The attributes specified in the other answers (and other ones such as DebuggerNonUserCodeattribute) no longer work in the same way by default in Visual Studio 2015. The debugger will break on exceptions in methods market with those attributes, unlike in older versions of VS. To turn off the performance enhancement which changed their behaviour you need to change a registry setting:

DebuggerNonUserCode默认情况下,在 Visual Studio 2015中,其他答案中指定的属性(以及其他属性,如属性)不再以相同的方式工作。与旧版本的 VS 不同,调试器将在具有这些属性的方法市场中出现异常时中断。要关闭改变其行为的性能增强,您需要更改注册表设置:

reg add HKCU\Software\Microsoft\VisualStudio.0_Config\Debugger\Engine /v AlwaysEnableExceptionCallbacksOutsideMyCode /t REG_DWORD /d 1

More information can be found on the visual studio blog.

更多信息可以在Visual Studio 博客上找到。

(This should probably be a comment on the top answer but I don't have enough rep)

(这可能应该是对最佳答案的评论,但我没有足够的代表)