views:

341

answers:

9
+6  Q: 

how to use finally

I never properly understood the use of the finally statement. Can anyone tell me what the difference is between:

try {
    a;
    block;
    off;
    statements;
} catch (Exception e) {
    handle;
    exception;
    e;
} finally {
    do;
    some;
    cleanup;
}

on the one hand and:

try {
    a;
    block;
    off;
    statements;
} catch (Exception e) {
    handle;
    exception;
    e;
}
do;
some;
cleanup;

On the other

+2  A: 

In proper coding style you don't want to do a catch all as below.

try{
  [some task] 
}
catch
{
}

What you would want to do is catch specific known errors.

try{
  [some task] 
}
catch(Exception ex)
{
   if([known execption])
     [ignore]
   else
     throw(ex);
}
[Cleanup]

In this case your cleanup code will not be run in the case of an error being thrown again. So we add in the finally which will get run even if a new error is thrown.

try{
  [some task] 
}
catch(Exception ex)
{
   if([known execption])
     [ignore]
   else
     throw(ex);
}
finally
{
   [Cleanup]
}
Jonathan Park
+14  A: 

finally block executes always. finally block is used for cleanup, Like to free resources used within try catch... close db connections, close sockets are some examples. Even when unhandled exception occurs within your try catch block. Only time finnaly block doesn't execute is when system.exit() is called in try catch or some error occurs and not exception.

Error in above description means when Java application exit with conditions like Out Of Memory error. I see some one giving negative marking :( for this reason it seems.

YoK
+1 for especific use of finally.
Agusti-N
Correction: a finally block _is_ executed when a java.lang.Error is thrown. It might not execute successfully, but it does execute. Also, there are additional conditions that may thwart the execution of a finally block, see my answer for an incomplete list.
meriton
+1 for mentioning examples of finally feature
pramodc84
+12  A: 

The main difference is that the catch section might itself throw an exception, break out of a surrounding block, or return from the current method. In that case do; some; cleanup; is not executed.

With a finally block, it is guaranteed that that code will be executed.

Dan
Is finally guaranteed to be executed? http://stackoverflow.com/questions/464098/does-a-finally-block-always-run
David Basarab
Yes, not in case of System.exit(), that is true. Or, you know, a JVM crash.
Dan
... or a deadlock, or ... - the real rule is: A finally-block is guaranteed to have executed when/if the try-(catch)-finally statement completes.
meriton
+5  A: 

It's basically a bad idea to catch all exceptions - so you need to consider what will happen if an uncaught exception propagates up out of your try/catch or try/catch/finally block. Finally blocks allow you to clean up on the way out.

Also:

  • The catch block might throw an exception
  • You may want to return from the try block

In short, if you want some code to be executed when you leave the try/catch block however you're leaving it (aside from the process being terminated very hard), finally is your friend.

Jon Skeet
+5  A: 

The "finally" block will always execute.

In your second example, the cleanup would not occur if the catch block rethrows the exception, or if an uncaught exception occurred in the try block.

iandisme
There are numerous conditions where the finally is not executed (c.f. my answer), but you are correct that throwing exceptions (or using control flow statements) are not among them.
meriton
+2  A: 

From this forum on GeekInterview:

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Kyra
+1  A: 

Simply one line of explanation:

regardless whether you caught an exception or not, codes in the finally block will get executed.

the diff between the two pieces you gave here is: the codes in the piece without using finally will never get executed.

to properly understand finally, all you need to know is that finally = guarantee!

you can use it to clean up, to help w/ user friendliness or to retry something

ligerdave
A: 

In the first example the finally block always gets executed even if you have a return statement in the try clause. The only time it doesn't get executed is when you have System.exit(0).

Nada
there are numerous other conditions where the finally is not executed, c.f. my answer (which doesn't claim to give all of them ...)
meriton
+4  A: 

They differ if

  • the try-block completes by throwing a java.lang.Throwable that is not a java.lang.Exception, for instance because it is a java.lang.Error such as AssertionError or OutOfMemoryError.
  • the try-block completes abruptly using a control flow statement such a continue, break or return
  • the catch-block completes abruptly (by throwing any throwable, or using a control flow statement)

More generally, the java language guarantees that a finally block is executed before the try-statement completes. (Note that if the try-statement does not complete, there is no guarantee about the finally. A statement might not complete for a variety of reasons, including hardware shutdown, OS shutdown, VM shutdown (for instance due to System.exit), the thread waiting (Thread.suspend(), synchronized, Object.wait(), Thread.sleep()) or being otherwise busy (endless loops, ,,,).

So, a finally block is a better place for clean-up actions than the end of the method body, but in itself, still can not guarantee cleanup exeuction.

meriton