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 ...
๐ŸŒ
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
If we place the finally block before the catch block, the code will not compile. Lines 7โ€“8: The finally block contains the code that is always executed. The finally block is executed even when an exception is not thrown. This is useful for cleanup code that must always be executed, such as closing a file. ... Lines 7โ€“10: The error ArrayIndexOutOfBoundsException is thrown when trying to access an element of the array with an index that is out of bounds.
๐ŸŒ
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"); } } }
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.
๐ŸŒ
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:
๐ŸŒ
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 - In this tutorial, we will discuss the various keywords used in Java for Exception Handling such as Try, Catch, Finally, Throw and Throws with examples.
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
๐ŸŒ
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 ...
๐ŸŒ
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...
๐ŸŒ
ExamClouds
examclouds.com โ€บ home โ€บ java core
Handling Exceptions with Try-Catch and Finally Block in Java
A finally block should immediately follow the last catch block (or it must immediately follow the try block if there is no catch). Let's look at the simple example where an unchecked exception is caught. 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 world!!!"; 3.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ยท
๐ŸŒ
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.