Continuing in the Visual Studio debugger after an exception occurs

前端 未结 5 1195
星月不相逢
星月不相逢 2020-12-03 06:32

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.

相关标签:
5条回答
  • 2020-12-03 06:52

    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.

    0 讨论(0)
  • 2020-12-03 06:56

    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.

    This behaviour is by design.

    0 讨论(0)
  • 2020-12-03 07:08

    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.

    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.

    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.

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

    0 讨论(0)
  • 2020-12-03 07:08

    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 F5 will keep ramming into the same exception.

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

    This option is under menu ToolsOptionsDebuggingGeneral.

    0 讨论(0)
  • 2020-12-03 07:13

    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.

    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.

    0 讨论(0)
提交回复
热议问题