Strange finally behaviour? [duplicate]

That’s because you returned a value that was evaluated from q before you changed the value of q in the finally block. You returned q, which evaluated its value; then you changed q in the finally block, which didn’t affect the loaded value; then the return completed, using the evaluated value. Don’t write tricky code … Read more

Why do we use finally blocks? [duplicate]

What happens if an exception you’re not handling gets thrown? (I hope you’re not catching Throwable…) What happens if you return from inside the try block? What happens if the catch block throws an exception? A finally block makes sure that however you exit that block (modulo a few ways of aborting the whole process … Read more

In C# will the Finally block be executed in a try, catch, finally if an unhandled exception is thrown? [duplicate]

finally is executed most of the time. It’s almost all cases. For instance, if an async exception (like StackOverflowException, OutOfMemoryException, ThreadAbortException) is thrown on the thread, finally execution is not guaranteed. This is why constrained execution regions exist for writing highly reliable code. For interview purposes, I expect the answer to this question to be … Read more

return eats exception

The exception disappears when you use return inside a finally clause. .. Is that documented anywhere? It is: If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily … Read more

C++, __try and try/catch/finally

On Windows, exceptions are supported at the operating system level. Called Structured Exception Handling (SEH), they are the rough equivalent to Unix signals. Compilers that generate code for Windows typically take advantage of this, they use the SEH infrastructure to implement C++ exceptions. In keeping with the C++ standard, the throw and catch keywords only … Read more

Does a finally block always run?

from the Sun Tutorials Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues. I don’t … Read more