Java / Exception
Does return statement allow finally block to execute in Java?
Yes. finally runs on return, break, and most exceptions. It is skipped only if the JVM exits or the thread is stopped before finally starts.
Yes. The return statement executes the finally block.
public class FinallyReturn { public static void main(String[] args) { System.out.println("Value returned = " + myMethod()); } public static int myMethod() { try { return 0; } finally { return 1; } } }
Output: Value returned = 1
More Related questions...