In Java, a try { ... } finally { ... } is executed somewhat unintuitively to me. As illustrated in another question, Does finally always execute in Java?, if you have a retu
Though The finally block is made for closing resources, and a return statement is generally not supposed to be there, and Eclipse warns that "finally block does not complete normally", I found in some circumstances a "finally return" is still desirable.
ResponseType response = new ResponseType();
try{
//set some properties of the response object.
response.setStatus(1);
//return response;
}catch (Exception e){
//Some other properties of the response object according to the exception.
response.setStatus(0);
//return response;
}finally{
return response;
}
If I don't put the return clause in the finally block, I would have to repeat it in try and catch blocks and the current code is a little clearer.
If code in finally block ends abruptly, it changes return value/exception from try
block. This is considered to be bad practice, and you should not do that.
Among other places, this is also discussed in Java Puzzlers book.
Purpose of finally
:
Java people has created finally
block for closing operation,which has to be performed in both cases (e.g exception occurred or no exception).
It is not recommended to use return statement in finally
block as it overrides both the return
statement from catch
& try
block.
and warns "finally block does not complete normally"
finally
block should have return statement only when both try
and catch
has no return
statement.
Incase both try
and catch
have different value then we should not keep anything in finally
block,
try
and catch
block then its better to keep return in finally
and remove return in both try
& catch
block.finally
throws any exception then the return
inside that block will not execute.
finally
block will not execute only when:
If you call javac with -Xlint, then an appropriate warning will be generated indicating that you should not call return from a finally clause. For example (compiling a simple class with the above test()
method):
javac -Xlint foo.java
foo.java:13: warning: [finally] finally clause cannot complete normally
}
^
1 warning
Technically speaking, the return
in the try block won't be ignored if a finally
block is defined, only if that finally block also includes a return
.
It's a dubious design decision that was probably a mistake in retrospect (much like references being nullable/mutable by default, and, according to some, checked exceptions). In many ways this behaviour is exactly consistent with the colloquial understanding of what finally
means - "no matter what happens beforehand in the try
block, always run this code." Hence if you return true
from a finally
block, the overall effect must always to be to return true, no?
In general, this is seldom a good idiom, and you should use finally
blocks liberally for cleaning up/closing resources but rarely if ever return a value from them.
check this for reference Does finally always execute in Java?
"A try statement with a finally block is executed by first executing the try block. Then there is a choice:
If execution of the try block completes normally, [...]
If execution of the try block completes abruptly because of a throw of a value V, [...]
If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
If the finally block completes normally, then the try statement completes abruptly for reason R.
If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded)."