views:

133

answers:

7
int i=0;
try{
    int j = 10/i;
}
catch(IOException e){}
finally{
    Console.WriteLine("In finally");
    Console.ReadLine();
}

The finally block does not seem to execute when pressing F5 in VS2008. I am using this code in Console Application.

+6  A: 

The Visual Studio debugger halts execution when you get an uncaught exception (in this case a divide by zero exception). In debug mode Visual Studio prefers to break execution and give you a popup box at the source of the error rather than letting the application crash. This is to help you find uncaught errors and fix them. This won't happen if you detach the debugger.

Try running it in release mode from the console without the debugger attached and you will see your message.

Mark Byers
Already tried that...Updated the question.
Nadeem
Yeah without debugging i can see the message. So does this mean in debugging mode the finally will not execute.
Nadeem
@Nadeem: No, it doesn't mean that. I means that your exception is unhandled and that causes the debugger to break execution. If you handle the exception then things will work as you expect.
Mark Byers
Alright this means in the debug mode the execution gets stuck in such a case. And there is no way out to go to the finally block in such a situation.
Nadeem
there is, handle the other exceptions in a catch (Exception) {} block.
devnull
I suspect that if you hit F5 in VS and let the debugger carry on, you'll see the `finally` execute before the session ends.
Dan Puzey
Though strangely, it doesn't - Mark Byers is right - but I'm surprised it doesn't execute the `finally` block as the app drops out.
Dan Puzey
+1  A: 

F5 continues the application till the next breakpoint or unhandled exception.

I think you should use F10 rather for step debugging or turn on breaking for all exceptions (handled or not).

leppie
Doesn't work for either of F10 or F11
Nadeem
Are you compiling in Release mode?
leppie
tried in both release and debug
Nadeem
Are you sure you didnt change the project settings?
leppie
I just tested you exact code. What is the exact behavior you see? I get the debugger breaking on the dividebyzero. Then I step the intruction point 1 line forward, and the `finally` executes.
leppie
A: 

Don't run you application via F5. In Debug mode you can't skip exception, message box will pop-up again and again.

Instead build it and run via CMD, Far Manager, etc

abatishchev
So ultimately it means that in debug mode finally will not get executed.
Nadeem
Of course you can skip exceptions, by default it will skip all handled exceptions. Secondly, you can move the instruction pointer to the next line....
leppie
@Nadeem: Finally will always execute regardless.
leppie
A: 

Things to know about finally:

http://stackoverflow.com/questions/3421486/why-to-use-finally/3421507#3421507

Turek
A: 

As the final conclusion we all should agree, if there is an unhandled exception and the application is running in Debugging mode, finally won't get executed.

Nadeem
+2  A: 

If you want it to execute while debugging there are two things you can do:

1) Catch the correct exception:


    int i = 0;
    try
    {
        int j = 10 / i;
    }
    catch(DivideByZeroException e){}
    finally
    {
        Console.WriteLine("In finally");
        Console.ReadLine();
    }

2) Tell Visual Studio to ignore unhandled exceptions. Go to Debug-->Exceptions, and then you can uncheck the Common Language Runtime Exceptions "User-unhandled" option, or you can expand that node, and uncheck individual exception types.

JohnForDummies
A: 

I was having this problem, too, only I didn't realize it at first. I was testing my class's Dispose() when an exception was thrown in its using statement. I could not figure out why Dispose() was not getting called! It's because using statements are, more or less, try/finally's with the call to Dispose() in the finally block.

I actually ended up having to disable the JIT debugger by deleting registry keys before any of the solutions on this thread worked. I feel like there must have been a less drastic solution out there. Any suggestions for future reference?

Claire