Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - go back from exception

How do I deal with a program that is being debugged/ran in Visual Studio 2012 that shows an unhandled exception and the program stops (my program has an error)? Is there a way to go back a stack on the program to the previous line before the exception occurred?

Right now, whenever there is an exception in a program, I have to restart the program and catch (in my case, this takes long time). I want to go back to the previous line before the exception line after exception is thrown/shown in Visual Studio and go from there.

Is there any way to do this in Visual Studio? Any commands or buttons?

Like this:

line 1: object _hello = GetSomeObject()

line 2: if(_hello.Property == true) {
} // exception line - _hello is null

I want to go back to the line 1 in the same instance without stopping the debugging.

like image 773
iefpw Avatar asked Dec 16 '22 13:12

iefpw


2 Answers

Once the exception is thrown you can't backtrack, you will need to first use the call stack to identify how the exception occurs, and then set a break point just before it.

The next time you debug your code, when when the debugger hits your breakpoint, you can drag the arrow pointer up to a previous line to backtrack execution to that line, then use Watch/Quick Watch, etc.

The Call Stack can be accessed in VS2012 via Debug menu -> Other Windows -> Call Stack.

Once you have looked at the call stack you will know how your program reaches the exception condition and therefore where to set your breakpoint. It depends on your situation but, just using the call stack and intellisense may solve your problem without stepping through code.

like image 197
Kev Avatar answered Dec 21 '22 22:12

Kev


See here for more info about using the call stack.

The line at the top of the call stack is where your exception occurs.

If you click the second line from the top you can view the function or procedure calls occurred just before your exception line.

enter image description here

like image 23
03Usr Avatar answered Dec 21 '22 23:12

03Usr