如何在VS2010调试会话期间抛出异常
我有一个小问题。 有时,当我调试应用程序时,我想在调试会话期间模拟方法抛出的异常,但没有办法做到这一点。 我什至无法将光标(指示当前行的黄色光标)拖动到异常块。 在运行期间触发异常的唯一方法是更改当前代码行并编写我要抛出的异常。这种方法不好,因为我可能会忘记在下次运行时将其改回来。
I have a small issue.
Sometimes when I debug an application I want to simulate during the debug session an exception thrown from a method, but there is no way to do it.
I can't even drag the cursor (the yellow one that indicate the current line) to an exception block.
The only way to trigger an exception during the run is to change the current line of code and to write the exception I want to throw. This method is not good because I might forget to change it back in the next run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您正在阅读此答案,则可以使用 Visual Studio 2013,并且支持 64 位应用程序中的“编辑并继续”。如果您有 Visual Studio 2013 并且只是寻找一种到处抛出异常的方法来查看异常处理或错误页面如何工作,只需使用“编辑并继续”并直接键入代码即可。
单击“调试”>开始调试。您的网络应用程序启动。放置一个断点。单击您的应用程序,直到遇到断点。您现在可以自由地将 throw 语句添加到您的代码中!只需输入 throw new Exception("test") 或其他内容,保存文件,然后单击继续(高级调试器),它将处理您刚刚添加的行,就像您已编译它一样。
If you're reading this answer, Visual Studio 2013 is available and supports Edit and Continue in 64-bit applications. If you have Visual Studio 2013 and are just looking for a way to throw exceptions here and there to see how your exception handling or error pages work, just use Edit and Continue and type directly into your code.
Click Debug > Start Debugging. Your web app launches. Place a breakpoint. Click through your app until the breakpoint hits. You are now free to add throw statements right into your code! Just enter
throw new Exception("test")
or whatever, save the file, and click continue (advance your debugger) and it will process the line you just added as if you had compiled it.您可以使用
throw 并将其更改为 true。或者创建一个方法/属性并调用它们。
You could use
and change throw to true. Or create a method/property and call them.
您这样做是为了测试您正在处理调用代码中的异常吗?
如果是这样,您可能需要考虑编写一些单元测试并利用模拟框架来完成任务。
我有最小起订量(模拟)的经验
http://code.google.com/p/moq/
有一些关于站点来设置并运行它,一旦开始就非常容易。
您需要首先在解决方案中设置一个测试项目,添加 Moq dll,然后编写一个测试方法来设置对象的 Moq 并告诉它抛出异常。
为了让对象上的方法抛出异常,它看起来像这样。
我希望这有帮助。
Are you doing this to test that you are handling the exception in calling code?
If so you might want to look into writing some unit tests and utilize a mocking framework to pull tihs off.
I have experience with Moq (Mock)
http://code.google.com/p/moq/
There are some tutorials on the site to setup and run with it, it's pretty easy once you get going.
You'll need to setup a test project in your solution first, add the Moq dll's and then write a test method that sets up the Moq of your object and tells it to throw an exception.
It'll look something like this in order to have the method on your object throw an exception.
I hope this helps.
不幸的是,Visual Studio 仍然不支持从立即窗口抛出异常。
并且不支持 lambda 表达式、匿名类型、集合初始值设定项等。仅类似于 .NET 2.0。
您也可以使用 Debug.Assert(false);
Unfortunately, Visual Studio still doesn't support throwing an exception from Immediate Window.
As well as doesn't support lambda expressions, anonymous types, collection initializers, etc. Something like .NET 2.0 only.
Also you can use
Debug.Assert(false);
我发现在调试过程中从方法内抛出异常的唯一真正可行的方法是从执行过程中提取变量。即设置一个即将要用到的变量为null,显然一执行就会抛出异常。
然而,这不允许您抛出自定义异常(例如特定类型或消息)。
对于特定的异常我们需要根据之前的帖子修改代码。
The only really workable way I have found to throw exceptions from within a method during debugging is to pull variable from under the feet of the executing process. I.e. set a variable that is about to be used to null will obviously throw an exception as soon as it is executed.
However this will not allow you to throw custom exceptions (e.g. specific types or messages).
For specific exceptions we need to modify code according to previous posts.
您可以使用条件编译:
编辑:使用额外的条件:
您可以使用定义的上述条件(项目属性->构建->条件编译符号)创建自定义配置(类似于发布/调试)。
You could use conditional compilation:
EDIT: With an extra conditional:
You could create a custom configuration (similar to Release/Debug) with the above conditional defined (project properties->Build->Conditional Compilation Symbols).