Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding try catch finally with return and value that it returns

I have the following piece of code.

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
        return "Return finally value";
    }
}

The output for this is

Executing try
Executing finally
Return finally value

If I change my finally block to not return anything like

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
    }
}

Then the output is

Executing try
Executing finally
Return try value

Now I understand that finally is always executed except if we call system.exit(0); called or the JVM crashes. What I'm not able to understand is why the return value has changed ? I would still expect it to return the value of the try block.
Can anyone explain why the finally value is take into consideration and not the return value from the try block ?

Please refrain from answering because finally is executed even if there is an return in try block ... or finally doesn't execute only if there is a system.exit(0); called or the JVM crashes. as I know that.

EDIT :

(As per Dirk comment on this)
public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return printString("Return try value");
    } catch (Exception e){
        System.out.println("Executing Catch");
        return printString("Return catch value");
    } finally {
        System.out.println("Executing finally");
        return printString("Return finally value");
    }
}

private static String printString(String str){
    System.out.println(str);
    return str;
}

Output:

Executing try
Return try value
Executing finally
Return finally value
Return finally value
like image 645
StackFlowed Avatar asked Oct 30 '14 16:10

StackFlowed


People also ask

What happens if catch and finally both return value?

If both catch and finally return, the receiving method will get the returned value from the finally block.

Can we have a return statement in try catch and finally block?

Yes, we can write a return statement of the method in catch and finally block.

What will happen when catch and finally block both return value overridden?

When catch and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by catch block.

Can we return value in finally block?

Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.


1 Answers

Just before returning from the main block, the JVM has to make sure the finally block is executed, so it does that. The idea is to execute the finally block and then come back and execute the return statement from the main block. But if you have a return statement in the finally block, then it will be executed when the finally block is executed... which means that control never returns to the main block to complete the return statement.

  1. The JVM encounters the return statement in the main block. It pauses execution of the main block and checks for a finally clause.
  2. It executes the finally clause in its entirety, including its return statement.
  3. It never thus gets to complete the try block.

Note, however, that the try block's return expression is evaluated and then discarded. This is important if it has side effects. So if your main block has return i++ then this will have no effect on the return value but i will still get incremented. (Thanks to Dirk for pointing this out.)

like image 81
chiastic-security Avatar answered Nov 09 '22 00:11

chiastic-security