views:

425

answers:

7

When an unexpected exception occurs in your program (in the debugger). Sometimes you just want to skip it since killing the program at that point is more harmful than continuing. Or you just want to continue since you were more interested in another error/bug

Is there an option/compilerflag/secretswitch to enable this?

I understand exceptions should be resolved right away, but there are scenarios (like I described) where one just wants to skip it for the time-being

+2  A: 

Use a try-catch block, and when catching, don't do anything about the exception.

Sev
That won't work. A try without a catch or finally is invalid and a try/catch block which does nothing will not continue execution in the next line in the try< block when an exception occurs. You would have to wrap every line in your program with try/catch, which i ugly.
Maximilian Mayerl
+3  A: 

You can't do this without an appropriate catch block in your code, no. However, I can't remember ever wanting to do this: if an exception occurs which your code doesn't know how to genuinely handle, why would you want to continue? You're in a bad state at that point - continuing would be dangerous.

Can you give an example of why you'd want to continue in a debugger session but not in production code?

Jon Skeet
I stumbled onto this too - if the first exception is not important for the problem you're actually debugging, you may want to continue. In this case, you can reset the state to right before the exception and then skip the step that throws the exception. This may get much easier with the temporal debugging in VS 2010 :) In general, however, you're right, you have to handle the first exception anyway, sooner or later.
OregonGhost
@jon: very right.... yet I had this bug which was only happening in the production environment. So I hooked up the debugger... and yes there was the uncaught exception. And it wasn't even that big of a deal one, so I would have rather let the thing do it's job correctly first, and then cleanly stop the program fix the exception and put this back up.
Toad
@oregon: yes... the fact that the debugger catched it... indicates that the program is still floating around there..and it might be possible to let it continue. (just like asserts had this option)
Toad
@reinier: Assertions don't return anything, and shouldn't have side-effects - whereas suppose the exception is thrown by a method returning a value. The method isn't willing to return the value, because it's detected that the state is invalid... what should the calling method get? It's almost always better to stop the program and fix it than try to continue when in a bad state.
Jon Skeet
@jon: I agree it could be hairy. ;^) I'll take this as the answer: no it can't be done.
Toad
Disable every exception in the Debug->Exceptions window. Thus never letting the debugger catch your exceptions :-)
Jan Jongboom
@jan: which will make it crash right ;^)
Toad
"Can you give an example of why you'd want to continue in a debugger session but not in production code?" I'm debugging a web app. I want to let the request die, then run the request again and break earlier. But since I can't let it continue, I have to detach, reattach, and then send the request again.
Tom Ritter
+1  A: 

If you are into the debugger then right click on the line you want to continue and select: Set Next Statement... but use it at your own risk!

jmservera
Does this work if the exception has already occurred and I'm thrown in the debugger?
Toad
No, just if you started your application from the debugger, if it comes from JIT then it's not possible.
jmservera
+1  A: 

When stepping through the code in debug mode you could skip the execution of the instructions that throw the undesired exception. But if the exception is already thrown and you don't have a try/catch it will propagate.

Darin Dimitrov
A: 

Have a look at the Exception Handling Application Block and related documentation. It contains best practices for handling application exceptions and there is a lot of framework code done for you i.e. logging.

Burt
A: 

If you want to know what exception you want to allow. then you can do this below

try
{
       // your functionality
}
catch(Exception ex)
{
     // Catch only the exceptions you need to point out
}
finally
{
   //do what you want to complete with this function.
}
solairaja
yes I know how to catch an exception. My question is about that one exception which gets thrown but which I forgot to catch and the debugger catches it... this one I want to continue but I can't
Toad
CAN YOU TELL ME WHAT EXCEPTION DEBUGGER IS SHOWING AND WHICH PART OF THE CODE WITHE FEW LINES OF YOUR CODE
solairaja
if the exception is in the code me we can exclude. for sure.
solairaja
@solairaja: this is an hypothetical question. It has happened to me before that an exception was thrown, I didn't have a try/catch block around it, and the debugger kicked in. At that point I wanted to continue code execution but couldn't. This is my question: is it possible to resume anyways without first fixing the code and adding a try/catch block
Toad
Without FIXING Code or Try Catch Block we cannot exclude the error raised by the debugger. Since its hypothetical question, we dont know the exact error if we know the error, then we can exclude (still thats kind of fix only). So the final answer is "YOU CANNOT CONTINUE WITH THE EXCEPTION"
solairaja
@solairaja: THANK YOU VERY MUCH!
Toad
+1  A: 

I assume that by "skipping" you mean that you want your program to continue working after the exception. That, of course, is possible by catching the exception when using try-catch block.

If the exception is not application stopper (for example, some key variable is not initialized after the exception, and you can't continue work) it is recommended that you at least log it before continue. Of course, putting

catch (Exception e) { }

everywhere in your source will not lead to a stable application ;)

If your problem is more debugger-related (you don't want the debugger to stop on every thrown exception), then there is a place in VS where you can change this:

In the Debug menu, select Exceptions. You'll see all possible exceptions and you can adjust their behaviour when thrown or not handled by the user.

Danail
yes I know how to catch an exception. My question is about that one exception which gets thrown but which I forgot to catch and the debugger catches it... this one I want to continue but I can't
Toad
you mean, you have the app on your production environment, and you don't have access to change the source now. You want to make it work by just skipping that exception?If that is the case, hmmm. Not sure if you can tell the virtual machine "don't throw this exception"...
Danail
danail: the exception has already been thrown and the debugger catched it... The only thing I can do now is to view some variables, and exit the app/debugger. I would like (in case of a silly exception which doesn't matter anyway) be able to let the program continue on (starting from the next line/block)
Toad