Finally Block Execution After Return Statement in Java



Yes, the finally block will be executed even after a return statement within a try or catch block in a Java method.

Finally Block after the Return Statement

Usually, the return statement will be the last in a Java method. If you try to add any statements after the return in Java, an error will be generated. 

public class Sample {
   public String demoMethod() {
      System.out.println();
      return null;
      System.out.println("This statement will generate an error");
   }
   public static void main(String args[]) {
      new Sample().demoMethod();
   }
}

This will generate the following error -

Sample.java:5: error: unreachable statement
System.out.println("This statement will generate an error");
^
1 error

In Java, the finally block is a block of code that is executed at least once after the try or catch block, whether the exception was thrown or caught. These (try, catch, and finally) blocks handle the exceptions or errors that might occur during code execution.

If we call the System.exit() method explicitly, the finally block will not be executed. There are a few more situations where the finally block may not execute, such as:

  • If the Java Virtual Machine (JVM) crashes.
  • If there is a power failure.
  • If the software crashes unexpectedly.

Except for the above conditions, the final block will always be executed even after a return statement.

Example 1

In the following example, we use the "return statement" within the try and catch block in the count() method to prove that even after the return statement, the finally block will be executed:

public class FinallyBlockAfterReturnTest {
   public static void main(String[] args) {
      System.out.println(count());
   }
   public static int count() {
      try {
         return 1;
      } catch(Exception e) {
         return 2;
      } finally {
         System.out.println("Finally block will execute even after a return statement in a method");
      }
   }
}

The output shows that the final block is executed even after the return statement:

Finally block will execute even after a return statement in a method
1

Example 2

This is another similar example that verifies the behavior of the finally block in Java, which executes even after a return statement inside the try or catch block:

public class FinallyBlockAfterReturnTest  {
   public static void main(String[] args) {
      String result = getMessage();
      System.out.println("Returned Message: " + result);
   }
   
   public static String getMessage() {
      try {
         System.out.println("In try block");
         return "Message from try";
      } catch (Exception e) {
         System.out.println("In catch block");
         return "Message from catch";
      } finally {
         System.out.println("In finally block (executes always)");
      }
   }
}

The above program produces the following output:

In try block
In finally block (executes no matter what)
Returned Message: Message from try
Updated on: 2025-05-14T19:25:38+05:30

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements