Can we use "return" in finally block

JavaExceptionTry Catch-FinallyFinally

Java Problem Overview


Can we use return statement in finally block. Can this cause any problem?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRakesh KRView Question on Stackoverflow
Solution 1 - JavaAnkur LathiView Answer on Stackoverflow
Solution 2 - JavaKrushnaView Answer on Stackoverflow
Solution 3 - JavaSuresh AttaView Answer on Stackoverflow
Solution 4 - JavaPrasad KharkarView Answer on Stackoverflow