防止调试器打破第三方库中的所有异常 [英] Prevent debugger to break on all Exceptions in a 3rd party library

查看:99
本文介绍了防止调试器打破第三方库中的所有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保持一个在递归方法调用期间内部使用异常的开源库。这个例外是在调用堆栈中被收回的,在某些情况下被处理,而在其他情况下,它将被返回给调用者。



我们现在遇到的问题是一些用户正在运行Visual Studio Debugger,其中包含暂停所有异常选项,因此它们的调试器将在代码正常运行时停止。



除了告诉用户禁用该设置,有什么办法可以防止这种情况发生吗?喜欢,应用一些属性代码可能?

解决方案

这是非常可能的。需要做的是让Visual Studio将相关代码视为非用户代码,然后在调试器设置中启用Just my code选项:





几种方式可以说服Visual Studio,您的库不是用户代码。一个是简单地编译没有PDB文件的发布版本。另一个是使用



有一个演示项目显示这个动作:
https://bitbucket.org/rstarkov/demononusercode/src - 注意MyLibrary中的方法是如何标记的,用户代码属性。即使你告诉Visual Studio在所有例外情况下停止抛出,它仍然会跳过MyLibrary中的异常。



对于什么是值得的,我不认为你做错了。这是一个正确配置调试器的问题。没有使用任何例外,只是因为有人设置调试器停止在一切都不完全正确。


I maintain an open source library that internally uses exceptions during a recursive method call. The exception is taken back on the call stack and in some cases handled, while in others it will be returned to the caller.

The problem we are having right now is that some users are running Visual Studio Debugger with the "Halt on ALL exceptions" option, so their debugger will halt in our code while it is functioning normally.

Is there any way to prevent that from happening other than telling users to disable that setting? Like, applying some attribute to the code maybe?

解决方案

This is very much possible. All you need to do is to get Visual Studio seeing the relevant code as non-user code, and then enable the "Just my code" option in the debugger settings:

There are several ways of convincing Visual Studio that your library is not user code. One is to simply compile a release build without PDB files. Another is to mark your code with DebuggerNonUserCodeAttribute.

There’s a demo project showing this stuff in action: https://bitbucket.org/rstarkov/demononusercode/src – note how the methods in MyLibrary are marked with the non-user-code attribute. Even if you tell Visual Studio to stop on "Thrown" for all exceptions, it will still skip the exception in MyLibrary.

For what it’s worth, I do not consider what you’re doing to be wrong. It’s a question of configuring the debugger properly. Not using any exceptions just because someone has set their debugger to stop on everything does not exactly sound right.

这篇关于防止调试器打破第三方库中的所有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆