Is there a case when finally block does not execute in Java?


Questions related to Java exception handling are most frequent during interviews for many companies and even in exams. One such question that an interviewer might ask is whether there is a case when the finally block does not execute in Java. We will try to find the answer to this question in the simplest way possible. In general, the finally block is designed to execute regardless of whether an exception is thrown or handled in the try-catch blocks.

Is there a case when finally block does not execute in Java?

Before moving to the question, it is necessary to discuss Java Exception Handling first.

Exception Handling in Java

In Java, exception is an object that represents an error. The ocurred error disrupts the normal flow of execution of code blocks this is the reason why we need to handle these exceptions and the mechanism through which we handle these unexpected errors is called as Exception Handling.

Familiar examples of exceptions include ClassNotFoundException, FileNotFoundException, IOException and so forth.

Exception Handling mechanism works with the following five built-in keywords

  • try − It contains code block that might cause exceptions.

  • catch − It is used to handle the exception thrown by try block.

  • throw − It is used to throw a single exception explicitly.

  • throws − It can throw multiple exceptions.

  • finally − It contains crucial code blocks that must be executed.

The possible combinations of try block are

  • try-catch

  • try-finally

  • try-catch-finally

Example 

The following example shows the practical implementation of try, catch and finally blocks.

public class Finally {
   public static void main(String[] args) {
      try { // declaring try block
         System.out.println("Execution of Try block");
      } catch(Exception exp) { // defining catch block
         System.out.println(exp);
      }
      finally { // declaring finally block
         System.out.println("Execution of finally block");	
      }
   }
}

Output

Execution of Try block
Execution of finally block

In the above code, we can see that the try and finally block gets executed but the catch block didn't.

Finally Block

The previous example shows that whether the exception occurs or not the finally block will get executed. However, there exists a few scenarios where the finally block may not execute.

Cases when finally block may not execute

  • When we call System.exit() either inside try or catch block.

  • When the JVM encounters non-terminating code.

Let's understand these points with examples.

Example 

In this example, we will call System.exit() inside the try block to restrain the execution of finally block. When the JVM will encounter System.exit() the program will get terminated from that point, hence the finally block won't get executed.

public class Finally {
   public static void main(String[] args) {
      try { // declaring try block
         System.out.println("Execution of Try block");
         System.exit(0); // to terminate the program
      } catch(Exception exp) { // defining catch block
         System.out.println(exp);
      }
      finally { // declaring finally block
         System.out.println("Execution of finally block");	
      }
   }
}

Output

Execution of Try block

Example 

In the following example, we use a while loop along with a Boolean value true inside the try block which will create a infinite loop. This situation will block the execution of finally block.

public class Finally {
   public static void main(String[] args) {
      try { // declaring try block
         while (true) { // infinite loop
            // intentionally left blank
         }
      } catch(Exception exp) { // defining catch block
         System.out.println(exp);
      }
      finally { // declaring finally block
         System.out.println("Execution of finally block");	
      }
   }
}

The above code won't generate any output.

Conclusion

From the above discussion we can conclude that normally, the execution of finally block will always happen. However, we can create some situations where we can restrict its execution. In this article, we have shared two such scenarios with the help of example programs.

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 10-Aug-2023

575 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements