C# 发生异常后继续在 Visual Studio 调试器中

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

Continuing in the Visual Studio debugger after an exception occurs

c#debuggingexception

提问by vIceBerg

When I debug a C# program and I get an exception throwed (either thrown by code OR thrown by the framework), the IDE stops and get me to the corresponding line in my code.

当我调试 C# 程序并抛出异常(由代码抛出或由框架抛出)时,IDE 会停止并将我转到代码中的相应行。

Everything is fine for now.

目前一切都很好。

I then press "F5" to continue. From this moment, it seams like I'm in an infinite loop. The IDE always get me back to the exception line. I have to Shift+ F5(stop debugging/terminate the program) to get out of his.

然后我按“F5”继续。从这一刻开始,我仿佛陷入了无限循环。IDE 总是让我回到异常行。我必须Shift+ F5(停止调试/终止程序)才能摆脱他。

I talked with some co-workers here and they told me that this happens sometime to them too.

我和这里的一些同事交谈过,他们告诉我这种情况有时也会发生在他们身上。

What's happening?

发生了什么?

采纳答案by Eric Schoonover

This is because the exception is un-handled and Visual Studio can not move past that line without it being handled in some manner. Simply put, it is by design.

这是因为未处理异常并且 Visual Studio 无法在没有以某种方式处理的情况下移过该行。简单地说,这是设计使然。

One thing that you can do is drag and drop the execution point (yellow line/arrow) to a previous point in your code and modify the in memory values (using the Visual Studio watch windows) so that they do not cause an exception. Then start stepping through the code again1.

您可以做的一件事是将执行点(黄线/箭头)拖放到代码中的前一个点并修改内存中的值(使用 Visual Studio 监视窗口),以便它们不会导致异常。然后再次开始单步执行代码1

It is a better idea though to stop execution and fix the problem that is causing the exception, or properly handle the exception if the throw is not desired.

最好停止执行并修复导致异常的问题,或者在不需要抛出时正确处理异常。

1This can have unintended consequences since you are essentially re-executing some code (not rewinding the execution).

1这可能会产生意想不到的后果,因为您实际上是在重新执行某些代码(而不是倒退执行)。

回答by Peter Hession

Once you get an exception Visual Studio (or whatever IDE you might be using) will not let you go any further unless the exception is handled in your code.

一旦您遇到异常,Visual Studio(或您可能使用的任何 IDE)将不会让您继续前进,除非您的代码中处理了该异常。

This behaviour is by design.

此行为是设计使然。

回答by Mark Brackett

An uncaught exception will bring down your app. Instead of this, VS will just keep you at the uncaught exception, You will have to terminate or backwind your app.

未捕获的异常将导致您的应用程序崩溃。与此相反,VS 只会让您保持未捕获的异常,您将不得不终止或倒退您的应用程序。

回答by Joel Mueller

When the IDE breaks on the offending line of code, it stops just before executing the line that generated the exception. If you continue, it will just execute that line again, and get the exception again.

当 IDE 在有问题的代码行上中断时,它会在执行生成异常的行之前停止。如果继续,它只会再次执行该行,并再次获得异常。

If you want to move past the error to see what would have happened, had the error not occurred, you can drag the yellow-highlighted line (the line that will execute next) down to the next line of code after the offending one. Of course, depending on what the offending line failed to do, your program may now be in a state that causes other errors, in which case you haven't really helped yourself much, and probably should fix your code so that the exception either doesn't occur, or is handled properly.

如果您想跳过错误以查看会发生什么,如果错误没有发生,您可以将黄色突出显示的行(接下来将执行的行)拖到违规代码之后的下一行代码。当然,根据违规行未能执行的操作,您的程序现在可能处于导致其他错误的状态,在这种情况下,您并没有真正帮助自己,可能应该修复您的代码,以便异常不会不会发生,或者处理得当。

回答by Matt Woodard

You probably have the option "Unwind the callstack on unhandled exceptions" checked in Visual Studio. When this option is on Visual Studio will unwind to right before the exception, so hitting F5will keep ramming into the same exception.

您可能在 Visual Studio 中选中了“在未处理的异常上展开调用堆栈”选项。当这个选项打开时,Visual Studio 将在异常之前展开,因此命中F5将继续撞到同一个异常。

If you uncheck the option Visual Studio will break at the exception, but hitting F5will proceed past that line.

如果取消选中 Visual Studio 将在异常处中断的选项,但点击F5将继续超过该行。

This option is under menu ToolsOptionsDebuggingGeneral.

此选项位于菜单ToolsOptionsDebuggingGeneral 下