visual-studio Visual Studio 调试 - 在一个地方忽略异常,而在其他地方打破它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1644780/
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-10-22 11:10:10  来源:igfitidea点击:

Visual Studio debugging - ignore exception in one place while breaking at it elsewhere?

visual-studiodebuggingexception

提问by Kastaka

I have some code which generates a large quantity of ArgumentExceptions on one particular line (which is in a different developer's code, so I can't just change it), which are then caught and handled appropriately.

我有一些代码在一个特定的行(在不同的开发人员的代码中,所以我不能改变它)生成大量的 ArgumentExceptions,然后被捕获并适当地处理。

I'm trying to debug ArgumentExceptions which are happening in a different section of code (and are then caught and handled, so I can't just look at unhandled exceptions).

我正在尝试调试发生在不同代码部分的 ArgumentExceptions(然后被捕获和处理,所以我不能只查看未处理的异常)。

Is there some way to ignore the ArgumentExceptions originating from that particular other line of code, while still breaking on ArgumentExceptions which are thrown elsewhere?

是否有某种方法可以忽略源自该特定其他代码行的 ArgumentExceptions,同时仍然打破其他地方抛出的 ArgumentExceptions?

采纳答案by Kastaka

You might be able to do this, but it depends on how the code you want to debug is located relative to the other developer's code, and whether or not you can modify (but not commit your changes) to his code.

您也许可以这样做,但这取决于您要调试的代码相对于其他开发人员的代码的位置,以及您是否可以修改(但不能提交更改)他的代码。

The first thing you'll want to do is, at least temporarily, go to menu Tools-> Options-> Debuggingin Visual Studio, and tick the "Just My Code" box. I assume this is available even in Express editions, but it may not be, and if it's not available for you I'm afraid the rest of what I have to say probably won't help.

您要做的第一件事是,至少暂时,转到菜单工具->选项-> Visual Studio 中的调试,然后勾选“仅我的代码”框。我认为即使在 Express 版本中也可以使用它,但它可能不是,如果它对您不可用,恐怕我要说的其余部分可能无济于事。

Anyway, once you have that ticked, you will no longer see break-on-throw notifications for code that isn't "yours." This means code that is from an assembly not in your .sln, or code marked with the [DebuggerNonUserCode]attribute from System.Diagnostics. What I usually do then is temporarily decorate the offending methods with [DebuggerNonUserCode]until I'm done debugging what I need to debug, and then revert those changes before checking in to souce control.

无论如何,一旦你勾选了它,你将不再看到不是“你的”代码的中断通知。这意味着来自不在 .sln 中的程序集的代码,或标记有[DebuggerNonUserCode]属性 from 的代码System.Diagnostics。我通常做的是临时装饰有问题的方法,[DebuggerNonUserCode]直到我完成调试我需要调试的内容,然后在签入源控制之前恢复这些更改。

It's not as elegant as I'd like (I'd love a "never break on throws from this site again" checkbox in the exception assistant), but it's better than nothing.

它并不像我想要的那么优雅(我喜欢异常助手中的“永远不会再从这个网站抛出”复选框),但总比没有好。

I believe there may be other debugger settings that could interact with how "Just My Code" works, so if this doesn't work for you let me know and I'll try to get a more accurate picture of what my settings look like when I do this.

我相信可能还有其他调试器设置可以与“Just My Code”的工作方式进行交互,所以如果这对您不起作用,请告诉我,我会尝试更准确地了解我的设置在什么时候我这样做。

回答by JaredPar

If you are talking about the "Break On Throw" exception feature then no there is not. It is strictly a type based feature only and does not have any way to control for what section of the code throws the exception.

如果您在谈论“Break On Throw”异常功能,那么不存在。它严格来说只是一个基于类型的特性,无法控制代码的哪一部分引发异常。

Your best bet is to just place breakpoints on all of the lines which throw or temporarily suspend throwing an exception from the one place that you care about.

最好的办法是在所有抛出或暂时暂停从您关心的地方抛出异常的行上放置断点。

回答by Anthony Potts

If you know how you are calling it, I would set the break point in your code and then step into (F11) from there. You could also smack the programmer until they fix their code, which would have the effect of making you feel better (unless you are a pacifist) and maybe they won't have so many ArgumentExceptions in their code (which would probably make you feel better even if you are a pacifist).

如果您知道如何调用它,我会在您的代码中设置断点,然后从那里进入 (F11)。你也可以打程序员直到他们修复他们的代码,这会让你感觉更好(除非你是一个和平主义者)而且他们的代码中可能不会有这么多的 ArgumentExceptions(这可能会让你感觉更好)即使你是一个和平主义者)。

回答by Sam Harwell

Sounds like using exceptions as flow control. If the one you're trying to debug occurs later in the program, you can try to attach the debugger later, or you can wait until after the program is running to turn on breaking when an ArgumentException is thrown.

听起来像使用异常作为流量控制。如果您尝试调试的那个发生在程序的后面,您可以尝试稍后附加调试器,或者您可以等到程序运行后在抛出 ArgumentException 时打开中断。

Try to limit the scope as well - if the exception you're interested in derives from but is not exactly ArgumentException, break on that one instead.

也尝试限制范围 - 如果您感兴趣的异常源自但不完全是 ArgumentException,请改用该异常。

Tell the other developer to fix his code.

告诉其他开发人员修复他的代码。

Edit:In .NET 4, you can attach a handler to the AppDomain.FirstChanceExceptionevent, filter out non-ArgumentException excepitons and filter out the bad one based on the call stack.

编辑:在 .NET 4 中,您可以将处理程序附加到AppDomain.FirstChanceException事件,过滤掉非 ArgumentException 异常并根据调用堆栈过滤掉坏的异常。

回答by jrsconfitto

The links in the comments are great.

评论中的链接很棒。

i think Conditional Breakpoints are what you're looking for here. You can do this by right clicking your breakpoint and clicking the Condition... menu item.

我认为条件断点是您在这里寻找的东西。您可以通过右键单击断点并单击条件...菜单项来执行此操作。