有没有办法防止 Visual Studio 因特定方法中的异常而中断?
我知道我可以根据异常类型以及最终使用“异常”对话框捕获异常的事实来控制 Visual Studio 处理异常的方式。
但是,当我调用特定方法时,我有一个库在内部抛出(并捕获)ArgumentOutOfRange 异常。抛出异常(并被库捕获)的概率可能为 1%,但我经常调用此方法。编辑说这是设计使然(事实上,他们选择的设计是有道理的)。
问题是我不希望 Visual Studio 每次抛出异常时都会中断。
- 我不想停止中断
ArgumentOutOfRange
异常,因为我的代码中可能有一些异常并且想要中断这些异常。 - 我不想启用“仅我的代码”调试,因为我担心代码之外引发的异常(特别是出于性能原因)
有没有办法实现此目的?我一直在研究属性(例如 DebuggerStepThrough),但还没有找到足够的东西。
关于如何执行此操作有任何提示吗?
I know I can control the way Visual Studio handles exceptions according to their type and to the fact that they're eventually caught using the "Exception" dialog.
However, I've got a library that's internally throwing (and catching) an ArgumentOutOfRange
exception when I'm calling a specific method. The exception is thrown (and caught by the library) maybe 1% of the time, but I'm calling this method a lot. The editor says it's by design (and indeed, the design they've chosen makes sense).
The thing is that I don't want Visual Studio to break each time the exception is thrown.
- I don't want to stop breaking on
ArgumentOutOfRange
exceptions, as I may have some in my code and want to break on those. - I don't want to enable "just my code" debugging because I'm concerned about the exceptions thrown outside of my code (notably for performance reasons)
Is there a way to achieve this? I've been looking into attributes (such as DebuggerStepThrough
), but haven't find something adequate yet.
Any hints on how to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,现在就到此为止。这正是您需要的功能,以避免不必要的调试器中断。如果您不想知道其他人的蹩脚代码,请重新打开该复选框。
当程序员使用异常进行流程控制时,这总是会偏离轨道。一种非常常见的犯罪行为。需要两个他们才能把事情变得一团糟,将调试会话变成非常乏味的点击噩梦。当您需要在第一次异常时中断的调试器功能时,如果其他人也需要它,那么您基本上就失败了。
每个人都希望他们能够神奇地使用 [DebuggerNonUserCode] 或 [DebuggerHidden] 或 [DebuggerStepThrough] 属性来使该问题消失。事实并非如此。另一位程序员并不认为他的代码不重要到值得拥有这些属性。而且,这并不是因为使用 try/catch-em-all 代码的代码中总是隐藏着一个错误。神奇宝贝代码。
因此微软必须找到另一种方法来帮助程序员处理蹩脚的库代码。他们做到了。勾选该复选框,砰,解决了。无论如何,除了向作者发送一个令人讨厌的代码之外,你对那些蹩脚的代码无能为力。不要让我们或微软拖慢您的脚步,你们必须共同努力才能创造出人们喜欢使用的产品。
Yeah, stop there right now. That is exactly the feature you need to not get the unwanted debugger breaks. If you don't want to know about somebody else's crappy code then flip that checkbox back on.
This invariably goes off the rails when programmers use exceptions for flow control. A very common crime. It takes two of them to turn that into a mess that turns a debugging session into a very tedious click nightmare. When you need the debugger feature that breaks on the first-chance exception then you basically lost if somebody else needed that as well.
Everybody hopes that they can magically use the [DebuggerNonUserCode] or [DebuggerHidden] or [DebuggerStepThrough] attributes to make that problem disappear. It doesn't. The other programmer did not think his code was unimportant enough to deserve those attributes. And, well, it wasn't because there's always a bug hidden in code that uses try/catch-em-all code. Pokémon code.
So Microsoft had to find another way to help programmers deal with crappy library code. They did. Tick that checkbox, bam, solved. Nothing you can do about that crappy code anyway, other than sending a nasty-gram to the author. Don't let us or Microsoft slow you down doing that as well, y'all have to get along to create a product that people like to use.
我认为这在 Visual Studio 中是不可能的,但在 WinDbg 中肯定是可行的。
请参阅示例 http://blogs.msdn.com/b/alejacma/archive/2009/08/24/management-debugging-with-windbg-writing-on-an-exception-part-1。顺便
说一句,从 Visual Studio 2010 开始,您似乎可以加载和使用 WinDbg 扩展 DLL 直接提供附加功能(包括可能您需要的功能),但我还没有尝试过 - 例如,参见http://www.dotnetcurry.com/ShowArticle.aspx?ID=648
I think it's not possible in visual studio but it certainly is in WinDbg.
See for example http://blogs.msdn.com/b/alejacma/archive/2009/08/24/managed-debugging-with-windbg-breaking-on-an-exception-part-1.aspx
On a side note it seems that starting with visual studio 2010 you can load and use WinDbg extension DLLs directly providing aditional functionality (including possibly the one that you need) but I haven't tried this yet - see for example http://www.dotnetcurry.com/ShowArticle.aspx?ID=648
您可以使用 Concord,它是 Visual Studio 附带的调试引擎(从 2012 版开始) 。它可以通过良好的托管 API 进行扩展(并且可以使用 vsix 技术进行部署),但没有完整记录。
Concord 有调试监视器的概念,我们可以使用 IDkmDebugMonitorExceptionNotification 接口
最酷的是这个接口可以监控所有异常抛出。它还可以“抑制”任何检测到的异常事件,这正是我们所需要的。
我建议从 Hello World 示例开始: 。下载它,并确保它按您的预期运行。
现在,只需修改
HelloWorld.vsdconfigxml
如下:然后,只需创建一个 ExceptionHandler.cs 类并在其中放置类似的内容:
当您运行该项目时,您应该看到所有异常都受到监视(无论您的“只是我的代码'和/或异常触发设置),所以您只需要实现一些逻辑来抑制您真正不想看到的逻辑。我没有检查过,但我想您可以使用自定义属性构建逻辑,因为 Dkm 类提供了大量 .NET 元数据信息。
注意:正如您所看到的,有一些技巧可以确保程序正常终止。
What you can do is use Concord, the debug engine that ships with Visual Studio (starting with version 2012). It's quite extensible through a nice managed API (and deployable using vsix technology), but it's not fully documented.
Concord has the concept of debug monitors, that we can hook using the IDkmDebugMonitorExceptionNotification Interface
The cool thing is this interface can monitor all exceptions thrown. It can also "suppress" any detected exception event, which is exactly what we need.
What I suggest is to start with the Hello World sample: . Download it, and make sure it runs as expected for you.
Now, just modify
HelloWorld.vsdconfigxml
like this:Then, just create an
ExceptionHandler.cs
class and put something like this in there:When you run the project, you should see all exceptions being monitored (regardless of your 'just my code' and/or exception triggers settings), so what you just need to do is implement some logic to suppress the ones you really don't want to see. I've not checked but I suppose you could build your logic using custom attributes as the Dkm classes provide quite a lot of .NET metadata information.
Note: as you can see, there is some trickery to make sure the program will terminate normally.