Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 312
vIceBerg Avatar asked Oct 02 '08 20:10

vIceBerg


People also ask

How do I stop Visual Studio from breaking on exception?

To turn off stop on exceptions press " Ctrl + Alt + E ". This will open the Exceptions window . Untick "Common Language Runtime Exceptions - Thrown". That would prevent it from pausing from within the delegate, but not when it's rethrown on Wait .

How do I keep debugging in Visual Studio?

In most languages supported by Visual Studio, you can edit your code in the middle of a debugging session and continue debugging. To use this feature, click into your code with your cursor while paused in the debugger, make edits, and press F5, F10, or F11 to continue debugging.

How do I break an exception in Visual Studio?

Tell the debugger to break when an exception is thrownIn the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.

How do I fix unhandled exception in Visual Studio?

In Visual Studio, when exceptions are thrown or end up unhandled, the debugger can help you debug these by breaking just like it breaks when a breakpoint is hit.


2 Answers

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.

like image 124
Matt Woodard Avatar answered Sep 21 '22 15:09

Matt Woodard


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

like image 40
Eric Schoonover Avatar answered Sep 20 '22 15:09

Eric Schoonover