views:

53

answers:

3

I have some places in the C# application where potential Exception's are not catched, but there is a try-finally block to release resources before crashing in case of an Exception.

When I run the code in Visual Studio and an Exception occurs, it breaks at the corrsponding line, marks it yellow and describes the exception.

That's fine.

But after having noticed and read the exception, I want my application to fail savely (execute the finally blocks). This is exactly what would happen if I ran the code outside Visual Studio. However, when I hit F5 to continue, it gets stuck on that very line, marking it over and over again.

What can I do to tell Visual Studio I want the application to continue = fail?

A: 

Visual studio will continue on after breaking on exceptions if you do have a matching catch statement for the type of exception being thrown.

However, you can turn off the exception assistant completely for specific exceptions by going to the Debug > Exceptions menu and unchecking the exceptions you would like to ignore.

JamesMLV
No, I don't catch them, as I stated in my question. But I have a finally block.
chiccodoro
Hmm, if you don't catch it then what is the point of continuing to run the program? The debugger is simply telling you "it's dead, game over".
Hans Passant
@Hans, JamesMLV: Have you read my question? "... are not catched, but there is a try-finally block..." "...want my application to...execute the finally blocks..."
chiccodoro
I misunderstood at first. No way that I know of to keep going after VS stops you for an unhandled exception, but you can prevent it from stopping in the first place (see my update for a granular approach, or the other answer for an all-or-nothing)
JamesMLV
+4  A: 

What's happening is you're seeing the Exception Assistant feature of Visual Studio. Whenever there is an unhandled exception in user code it will display to inform the user of the problem.

Unfortunately in certain circumstances it can't be made to go away and you get the behavior you're describing. It's definitely very frustrating when that happens. The best way to work around it is to disable the exception assistant.

  • Tools -> Options
  • Debugging -> Generale
  • Uncheck "Enable the exception assistant"
JaredPar
+1  A: 

Sometimes I find I need to hit F5 several times. I know that it will stop on every rethrow or try-block that the exception goes through, so it may be that it is repeating because the exception is happening in a library function and is filtered through several try statements before leaving the library function. VS, however, will just show that library call several times. I've never gotten truly stuck, though. Hitting F5 a few times will get it moving again.

siride
Good to know! Will try that next time it happens for me.
chiccodoro
@siride: Had the same problem lately, and hitting F5 multiple times didn't help.
chiccodoro