They differ if

  • the try-block completes by throwing a java.lang.Throwable that is not a java.lang.Exception, for instance because it is a java.lang.Error such as AssertionError or OutOfMemoryError.
  • the try-block completes abruptly using a control flow statement such a continue, break or return
  • the catch-block completes abruptly (by throwing any throwable, or using a control flow statement)

More generally, the java language guarantees that a finally block is executed before the try-statement completes. (Note that if the try-statement does not complete, there is no guarantee about the finally. A statement might not complete for a variety of reasons, including hardware shutdown, OS shutdown, VM shutdown (for instance due to System.exit), the thread waiting (Thread.suspend(), synchronized, Object.wait(), Thread.sleep()) or being otherwise busy (endless loops, ,,,).

So, a finally block is a better place for clean-up actions than the end of the method body, but in itself, still can not guarantee cleanup exeuction.

Answer from meriton on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_keyword_finally.asp
Java finally Keyword
The finally keyword is used to execute code (used with exceptions - try..catch statements) no matter if there is an exception or not. Read more about exceptions in our Java Try..Catch Tutorial.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › finally.html
The finally Block (The Java™ Tutorials > Essential Java Classes > Exceptions)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. The finally block always executes when the try block exits.
🌐
DataCamp
datacamp.com › doc › java › finally
finally Keyword in Java: Usage & Examples
Learn how to use the `finally` block in Java for effective exception handling and resource management. Ensure your cleanup code always runs, regardless of exceptions. Examples and best practices included.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-use-finally-block-for-catching-exceptions
Java Program to Use finally block for Catching Exceptions - GeeksforGeeks
July 23, 2025 - In this case, the program throws an exception but handled by the catch block, and finally block executes after the catch block. ... // Java program to demonstrate finally block in java // When exception rise and handled by catch import java.io.*; class GFG { public static void main(String[] args) { try { System.out.println("inside try block"); // Throw an Arithmetic exception System.out.println(34 / 0); } // catch an Arithmetic exception catch (ArithmeticException e) { System.out.println( "catch : exception handled."); } // Always execute finally { System.out.println("finally : i execute always."); } } }
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java finally keyword
Guide to the Java finally Keyword | Baeldung
January 16, 2024 - Java's finally keyword is helpful for clean-up operations around code that may throw errors. We explore how it works and when it can have unexpected behaviour.
🌐
Tutorialspoint
tutorialspoint.com › java › java_finally_block.htm
Java - Finally Block
Java Vs. C++ ... The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception. Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.
Top answer
1 of 10
47

They differ if

  • the try-block completes by throwing a java.lang.Throwable that is not a java.lang.Exception, for instance because it is a java.lang.Error such as AssertionError or OutOfMemoryError.
  • the try-block completes abruptly using a control flow statement such a continue, break or return
  • the catch-block completes abruptly (by throwing any throwable, or using a control flow statement)

More generally, the java language guarantees that a finally block is executed before the try-statement completes. (Note that if the try-statement does not complete, there is no guarantee about the finally. A statement might not complete for a variety of reasons, including hardware shutdown, OS shutdown, VM shutdown (for instance due to System.exit), the thread waiting (Thread.suspend(), synchronized, Object.wait(), Thread.sleep()) or being otherwise busy (endless loops, ,,,).

So, a finally block is a better place for clean-up actions than the end of the method body, but in itself, still can not guarantee cleanup exeuction.

2 of 10
32

finally block executes always.

finally block is used for cleanup, like to free resources used within try/catch, close db connections, close sockets, etc.. even when an unhandled exception occurs within your try/catch block.

The only time the finally block doesn't execute is whensystem.exit() is called in try/catch or some error occurs instead of an exception.

The error in the description above means when Java application exit with conditions like Out Of Memory error. I see some downvotes :( for this reason it seems.

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-final-finally-and-finalize
Java final, finally and finalize - GeeksforGeeks
January 6, 2016 - In Java, the keywords "final", "finally" and "finalize" have distinct roles. final enforces immutability and prevents changes to variables, methods or classes. finally ensures a block of code runs after a try-catch, regardless of exceptions.
Find elsewhere
🌐
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 - A finally block of code is always executed whether an exception has occurred or not. Using a finally block, it lets you run any cleanup type statements that you want to execute, no matter what happens in the protected code.
🌐
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.
🌐
Medium
medium.com › @AlexanderObregon › how-javas-try-finally-blocks-work-without-catch-96e93be92ae5
How Java’s try-finally Blocks Work Without catch | Medium
March 4, 2025 - Even though no catch block is present, the finally block still runs before the exception message appears. This happens because Java guarantees that finally executes before control exits the try-finally construct. Method Exits with a return Statement When a return statement is inside the try block, it does not immediately exit the method.
🌐
Reddit
reddit.com › r/learnjava › purpose of “finally”?
r/learnjava on Reddit: Purpose of “finally”?
November 8, 2023 -

Maybe I’m just too early in learning Java to understand the reason you’d use it and for my purposes it’s irrelevant, but I’m curious.

I know it executes regardless of whether an exception is caught but doesn’t the code below a try {} catch {} block also execute regardless of whether an exception is caught? Isn’t that PART of the point of catching the exception? For the code to continue?

Forgive my ignorance, as there may be some serious misunderstandings of concepts in this post.

Thank you.

Top answer
1 of 8
8
finally is useful if you want to run a bit of code regardless of whether you catch an exception, rethrow an exception, or don't catch an exception at all. Examples include closing resources like files regardless of whether you succeed or error. FileOutputStream stream = new FileOutputStream(...); try { // do stuff with stream that might throw an IOException } finally { // if we throw an exception of any kind, or if we succeed without // an issue, then this always gets run stream.close(); } A long time ago, Java introduced the AutoCloseable and Closable interfaces which let you do this in a bit cleaner way, which looks like this: try (FileOutputStream stream = new FileOutputStream(...)) { // do stuff with stream that might throw an IOException } This does the same thing as my first code block, but is called a "try with resources" and works on anything that implements AutoCloseable or Closeable. Notice I didn't catch any exception. I'm simply specifying that I always want it to do something whether I throw an exception or not. You can mix this with catch blocks if you want in more complex situations too. Also worth noting if you return in a try block, the finally will still get run. try { return 123; } finally { System.out.println("yeet"); }
2 of 8
6
Because sometimes you don't want the code to continue. A lot of the time we log the error and throw a new one to stop the process. Because t still need to close files and other things either way. That's what the finally is for.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › finally_keyword_in_java.htm
Java - finally Keyword
Java finally keyword is used to define a finally block in a Java program. The finally block in Java follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception in a Java program.
🌐
Scaler
scaler.com › topics › java › try-catch-and-finally-in-java
Try, Catch and Finally in Java | Scaler Topics
August 29, 2022 - When we have nested catch blocks, as soon as a catch matches the exception thrown, it executes the code inside it and then other catch blocks are ignored by the execution. The finally block (if present) is then executed and the execution continues. In Java, all exception classes extend the class Throwable.
🌐
Object Computing
objectcomputing.com › resources › publications › sett › may-2000-an-integral-part-of-exception-handling-finally
An Integral Part of Exception Handling, Finally | Object Computing, Inc.
For Java developers, the try-catch block is a way of life. A fundamental part of this mechanism is the finally block. As the name suggests, code in a finally block is always executed last.
🌐
Programming.Guide
programming.guide › java › try-finally.html
Java: try + finally | Programming.Guide
Executing code in comments?! ... A finally block is always executed after the code in the preceeding try block. It doesn't matter if the try block throws an exception, whether or not the exception is caught, or if it executes a return statement.