will finally block get executed if return?

Yes, finally block will always be executed.

Finally block will always execute except the below cases: Call the System.exit() method explicitly, power failure, JVM crash, software crash etc.

public class Main {
 
   String getWebsiteName() { 
      try {
         return "w3spoint.com";
      } catch(Exception e) {
         return "java.com";
      } finally {
         System.out.println("Finally block executed even after a return statement.");
      }
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      System.out.println(main.getWebsiteName());
   }
 
}

Output

Finally block executed even after a return statement.
w3spoint.com

Java interview questions on Exception Handling

Please follow and like us:
Content Protection by DMCA.com