The only time a finally block will not be executed is when you call exit() before finally is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run.

EDIT: This is not entirely correct. See the comments below for additional information.

Answer from Gabriel Negut on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
GeeksforGeeks
geeksforgeeks.org › java › flow-control-in-try-catch-finally-in-java
Flow control in try catch finally in Java - GeeksforGeeks
July 23, 2025 - ... // Java program to demonstrate try-catch // when an exception doesn't occurred in try block class GFG { public static void main (String[] args) { try { String str = "123"; int num = Integer.parseInt(str); // this statement will execute // ...
🌐
Programiz
programiz.com › java-programming › try-catch
Java try...catch (With Examples)
Here, the size of the array is 5 and the last element of the array is at list[4]. However, we are trying to access elements at a[5] and a[6]. Hence, the code generates an exception that is caught by the catch block. Since the finally block is always executed, we have included code to close ...
🌐
Medium
medium.com › @bhangalekunal2631996 › exploring-all-possible-combinations-of-try-catch-finally-in-java-a-complete-guide-with-examples-fc36bf20d3de
Exploring All Possible Combinations of Try-Catch-Finally in Java: A Complete Guide with Examples | by Kunal Bhangale | Medium
January 3, 2025 - Explanation: Code cannot appear between catch and finally. ... public class Example12 { public static void main(String[] args) { try { System.out.println("First try block"); } catch (Exception e) { System.out.println("Caught Exception in first try"); } try { System.out.println("Second try block"); } finally { System.out.println("Finally block for second try"); } } }
🌐
Scaler
scaler.com › topics › java › try-catch-and-finally-in-java
Try, Catch and Finally in Java | Scaler Topics
August 29, 2022 - Try and catch blocks are the same as the above examples, but we can see that the code in the finally block is executed. Even if there is no occurrence of an exception, it will still be executed as shown below. Example of Finally Block with no exceptions thrown in the try block: ... We can see that no exception thrown is the case in the try block, but the code in the finally block is still executed. In Java, we have some pre-defined exceptions known as Built-in Exceptions, which are accessible using libraries.
🌐
Jenkov
jenkov.com › tutorials › java-exception-handling › basic-try-catch-finally.html
Basic try-catch-finally Exception Handling in Java
The exception is propagated up the call stack like this until some method catches the exception, or the Java Virtual Machine does. You can attach a finally-clause to a try-catch block.
🌐
Educative
educative.io › answers › how-to-use-the-try-catch-and-finally-blocks-in-java
How to use the try, catch, and finally blocks in Java
The finally block is used to execute the code after the try and catch blocks have been executed. The following code snippet shows how to use these blocks: ... Lines 3–5: The try block contains the code that may throw an exception.
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › exception handling › java try catch finally
Java try catch finally (with Examples) - HowToDoInJava
April 7, 2023 - If there is an exception occurred in try block, then JVM will execute catch block first and then finally block.
Top answer
1 of 2
150

If the return in the try block is reached, it transfers control to the finally block, and the function eventually returns normally (not a throw).

If an exception occurs, but then the code reaches a return from the catch block, control is transferred to the finally block and the function eventually returns normally (not a throw).

In your example, you have a return in the finally, and so regardless of what happens, the function will return 34, because finally has the final (if you will) word.

Although not covered in your example, this would be true even if you didn't have the catch and if an exception were thrown in the try block and not caught. By doing a return from the finally block, you suppress the exception entirely. Consider:

public class FinallyReturn {
  public static final void main(String[] args) {
    System.out.println(foo(args));
  }

  private static int foo(String[] args) {
    try {
      int n = Integer.parseInt(args[0]);
      return n;
    }
    finally {
      return 42;
    }
  }
}

If you run that without supplying any arguments:

$ java FinallyReturn

...the code in foo throws an ArrayIndexOutOfBoundsException. But because the finally block does a return, that exception gets suppressed.

This is one reason why it's best to avoid using return in finally.

2 of 2
107

Here is some code that show how it works.

class Test
{
    public static void main(String args[]) 
    { 
        System.out.println(Test.test()); 
    }

    public static String test()
    {
        try {
            System.out.println("try");
            throw new Exception();
        } catch(Exception e) {
            System.out.println("catch");
            return "return"; 
        } finally {  
            System.out.println("finally");
            return "return in finally"; 
        }
    }
}

The results is:

try
catch
finally
return in finally
🌐
TutorialsPoint
tutorialspoint.com › Flow-control-in-a-try-catch-finally-in-Java
Flow control in a try catch finally in Java
The following is an array declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception. // File Name : ExcepTest.java import java.io.*; public class ExcepTest { public static void main(String args[]) { try { int a[] = new int[2]; System.out...
🌐
Medium
medium.com › thefreshwrites › try-catch-finally-in-java-exception-handling-2ed273814cd8
Try-Catch-Finally In Java | Exception Handling | by Mouad Oumous | The Fresh Writes | Medium
February 22, 2024 - See the example on the right. Catch blocks must come directly after the try block they are paired with. Don’t forget to add import statements for your exception types as they are not part of the standard Java library. ... A finally keyword is used to create a block of code that follows a ...
🌐
Javatpoint
javatpoint.com › finally-block-in-exception-handling
Java Finally block - javatpoint
Java finally block is a block that is always executed. Let's see its application and example.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-try-catch-block
Java Try Catch Block - GeeksforGeeks
June 3, 2025 - } finally { System.out.println("I will always execute whether an Exception occur or not"); } Example: Here, we demonstrate the working try catch block with multiple catch statements. Java ·
🌐
ExamClouds
examclouds.com › home › java core
Handling Exceptions with Try-Catch and Finally Block in Java
In line 4 StringIndexOutOfBoundsException is thrown, and the execution is transferred to line 6. So the line 5 is never executed in this example. 1. try { 2. String str = "Hello Java ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › try, catch, finally and throw in java with examples
Try, Catch, Finally And Throw In Java With Examples
April 1, 2025 - But as try block exits after the exception is raised we cannot put this code in the try block. Similarly, the catch block has an exception handler, so we cannot put this in the catch block too. Thus we need a separate block that contains a code that executes irrespective of whether an exception occurs or not. Java provides a “finally” block that contains this piece of code.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › finally.html
The finally Block (The Java™ Tutorials > Essential Java Classes > Exceptions)
Important: Use a try-with-resources statement instead of a finally block when closing a file or otherwise recovering resources. The following example uses a try-with-resources statement to clean up and close the PrintWriter and FileWriter for the writeList method:
🌐
Board Infinity
boardinfinity.com › blog › try-catch-finally-in-java
Try, Catch and Finally in Java | Board Infinity
August 16, 2025 - Master exception handling in Java with try, catch, and finally blocks. Learn how to manage errors and ensure safe, stable programs.
🌐
DataCamp
datacamp.com › doc › java › try
try Keyword in Java: Usage & Examples
Here, the try block contains code that throws an ArrayIndexOutOfBoundsException. The catch block handles this exception, and the finally block executes regardless of the exception.