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.
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
If both catch and finally return, the receiving method will get the returned value from the finally block.
Yes, we can write a return statement of the method in catch and finally block.
When catch and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by catch 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.
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.
return
statement in the main block. It pauses execution of the main block and checks for a finally
clause.finally
clause in its entirety, including its return
statement.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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With