Can we use return statement in finally block. Can this cause any problem?
-
You might also go through [**this post**](http://stackoverflow.com/q/18131447/1679863) – Rohit Jain Aug 13 '13 at 09:40
4 Answers
Returning from inside a finally
block will cause exceptions
to be lost.
A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded.
According to the Java Language Specification:
If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and 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).
Note: As per JLS 14.17 - a return statement always completes abruptly.

- 7,636
- 5
- 37
- 49
-
the JLS quote does not support your claim about return statement inside finally block? – linski Aug 13 '13 at 10:06
-
These lines from JLS are for exceptions but these are also true for return statement. – Ankur Lathi Aug 13 '13 at 11:32
-
I still don't get the relation between the quote and your statement- so I wrote [a piece of code](http://ideone.com/1GaeBo) which shows that, when the finally block contains return statement, exceptions thrown from catch block get eaten, but the ones thrown from try block get caught. Please verify. – linski Aug 13 '13 at 12:03
-
2Go through this code http://ideone.com/AU9KvS . Your Exception from try is eaten. – Ankur Lathi Aug 13 '13 at 12:18
-
I completely missed the variant without the catch block! Thank you, upvoted already :) – linski Aug 13 '13 at 12:23
-
The Java Language Specification is clear about the return: The return statement always completes abruptly. (reference: http://thegreyblog.blogspot.co.uk/2011/02/do-not-return-in-finally-block-return.html) – Exploring Jan 06 '14 at 12:19
-
4You should add to your answer that `return` is considered as abrupt completion by Java spec. – kolobok Apr 14 '17 at 22:02
-
Yes you can write the return statement in a finally block and it will override the other return value.
EDIT:
For example in below code
public class Test {
public static int test(int i) {
try {
if (i == 0)
throw new Exception();
return 0;
} catch (Exception e) {
return 1;
} finally {
return 2;
}
}
public static void main(String[] args) {
System.out.println(test(0));
System.out.println(test(1));
}
}
The output is always 2, as we are returning 2 from the finally block. Remember the finally always executes whether there is a exception or not. So when the finally block runs it will override the return value of others. Writing return statements in finally block is not required, in fact you should not write it.

- 8,198
- 71
- 51
- 66

- 5,059
- 5
- 32
- 49
-
-
1try{ throws new exception(); } catch{ throws new exception();} finally{ return 5; } Here What happends??? – Rakesh KR Aug 13 '13 at 09:39
-
1
-
1It may not work fine if a System.exit(0) call is done from try or the catch block. The control will not go to finally block in that case. – Jatin Sehgal Aug 13 '13 at 10:00
-
System.exit(0) prevent the execution of finally block as the JVM is going to shut down, no matter what return – Krushna Aug 13 '13 at 10:02
-
@RakeshKR it will return 5, because exception that thrown by catch block it will handled by finally block, and if you remove finally block so in given case it will give compile time error, so either you add finally block or explicitly you will need to handle it. – Mohammad Faizan Dec 06 '17 at 08:57
-
This is crazy. I was wondering for years why in Listing 7.12 "Encapsulating nonstandard cancellation in task with newTaskFor" in the Java Concurrency in Practice he would `return super.cancel(mayInterruptIfRunning);` specifically in finally block. Thanks! – vitrums Jan 02 '21 at 11:19
Yes you can,But you should not 1 ,because the finally block is meant for a special purpose.
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.
Writing your logic inside it is not recommended.

- 120,458
- 37
- 198
- 307
-
1This does not really explain much. TJ Crowder's answer here is much better - https://stackoverflow.com/questions/15225819/try-catch-finally-return-clarification – Erran Morad Dec 05 '17 at 03:44
-
@BoratSagdiyev If it is too detailed ..please go and read about it. Thanks – Suresh Atta Dec 05 '17 at 06:20
You can write return
statement in finally
block but the value returned from try
block will be updated on the stack and not the finally
block return value.
Let us say you have this function
private Integer getnumber(){
Integer i = null;
try{
i = new Integer(5);
return i;
}catch(Exception e){return 0;}
finally{
i = new Integer(7);
System.out.println(i);
}
}
and you are calling this from main method
public static void main(String[] args){
System.out.println(getNumber());
}
This prints
7
5

- 13,410
- 5
- 37
- 56
-
1The question is about if we can return a value from finally block or not. Remember return statement in finally will override the other return values. Consider the below code snippet: class ReturnClass { public int testValue() { try { return 3; } catch(Exception e) { } finally { return 5; } } public static void main(String ar[]) { ReturnClass rc = new ReturnClass(); System.out.println(rc.testValue());} } Output will be always 5. – gkbstar Jun 13 '18 at 12:28
-
2This is off topic. It got me confused until I carefully read that you are not `return`ing the control from `finally`, rather just printing something from there. – Nikhil Girraj Jun 18 '18 at 04:41