vIceBerg
vIceBerg

Reputation: 4327

Continuing in the Visual Studio debugger after an exception occurs

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.

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.

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

What's happening?

Upvotes: 68

Views: 24399

Answers (8)

Daniel Lobo
Daniel Lobo

Reputation: 2211

In 2025 this happened to me in a new installation of Microsoft Visual Studio Community 2022. For some reason the default installation doesn't allow you to bubble up the exception, which is exactly what I want to check where is it going afterwards: a new exception handler or is catastrophic.

What is missing is just on the (Debug->Windows) "Exception Settings" screen the right click on the "Common Language Runtime Exceptions" and to choose "Continue when unhandled in user code"

I hope it helps.

enter image description here

Upvotes: 0

Protector one
Protector one

Reputation: 7331

In an ASP.NET application, as a workaround, you could call Response.End() from the Immediate Window, and then continue the debug session. You won't get served the error page in the browser like before, but at least now you can try another request without having to restart the debug session.

I really have no idea when and why Microsoft changed this behavior. Would love to know if there is a setting or something to revert back to the old default behavior.

Upvotes: 2

Cesar
Cesar

Reputation: 615

Some said that is by design, but it was never been before. You were able to F5 again and the code would continue until the end or next exception catch.

It was an useful behavior that worked well, and as developers, I think we should not reach artificial barriers while we are debugging and investigating issues in an application.

That said, I found a workaround to this (sort of):

  • press Ctr+Shift+E (Exception settings)
  • uncheck the "Common Language Runtime Exceptions" box
  • press F10 to continue only the exception you are stuck
  • check "Common Language Runtime Exceptions" again, if you want the breaks to happen again

That seems dirty, too much work for a thing that is used to be simpler, but I guess that is what we have for today.

Upvotes: 14

Matt Woodard
Matt Woodard

Reputation: 2076

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.


Update: According to Microsoft, this option was removed from Visual Studio in VS2017, and maybe earlier.

Upvotes: 49

Eric Schoonover
Eric Schoonover

Reputation: 48412

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).

Upvotes: 20

Joel Mueller
Joel Mueller

Reputation: 28764

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.

Upvotes: 6

Mark Brackett
Mark Brackett

Reputation: 85685

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.

Upvotes: 2

Peter Hession
Peter Hession

Reputation: 499

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.

Upvotes: 2

Related Questions