Yes, the finally block will be executed even after the return statement in the method.
In Java, the finally block will be executed regardless of whether an exception occurs. If we explicitly call the System.exit() method in the finally block, then only it will not be executed. There are rare situations where finally will not be executed, such as JVM crash, power failure, software crash, etc. Except for these cases, the finally block will always be executed.
public class FinallyBlockAfterReturnTest { public static void main(String[] args) { System.out.println(<strong>count()</strong>); } public static int count() { try { <strong> return 1; </strong> } catch(Exception e) { <strong> return 2; </strong> } finally { System.out.println("Finally block will execute even after a return statement in a method"); } } }
Finally block will always excute even after a return statement in a method 1
The above is the detailed content of In Java, after the return statement in a method is executed, will the finally block be executed?. For more information, please follow other related articles on the PHP Chinese website!