Return in a finally blockWhat will happen if we return a value in a finally block?
If the finally block executes successfully the return in the finally block will override the return value set in the try-catch block.
Java
package org.magicworldz.basic.finallyreturn;
public class FinallyReturn {
public static void main(String[] args) {
var app = new FinallyReturn();
System.out.println(app.finallyReturn(5));
}
public int finallyReturn(int ret) {
try {
return ret;
} finally {
return 0;
}
}
}
Call the finallyReturn(2)
will get 0
.
The finally block will execute no matter exception handled or not. But in some special cases, the finally block will not been executed.
- When an exception occurs in the first line of the finally block. It will execute if an exception occurs in other lines.
System.exit(int)
is invoked.- The thread dies
- Close the CPU