1. Introduction
The finally block is an optional part of a try statement that always executes after the try block (and any matching catch block) completes, regardless of whether an exception was thrown or caught. It is most commonly used for cleanup tasks like closing files, releasing database connections, or freeing other resources.
Cricket analogy: The finally block is like the mandatory post-match pitch inspection that always happens whether the game ended in a win, loss, or was abandoned for rain, used for cleanup tasks like covering the ground.
Understanding exactly when finally runs — including its interaction with return statements — is essential for writing correct resource-management code and is a frequent source of tricky exam questions.
Cricket analogy: Understanding finally's interaction with return is like knowing that even after a batsman is given out (return), the third umpire's review (finally) still runs and can affect what's officially recorded before the scoreboard updates.
2. Syntax
try {
// risky code
} catch (SomeException e) {
// handle exception
} finally {
// cleanup code — always runs
}3. Explanation
The finally block executes after the try block completes normally, after a catch block finishes handling an exception, or even if the try/catch block contains a return statement. Java evaluates the return value from try/catch first, but actually returns control to the caller only after finally has fully executed.
Cricket analogy: Finally executes whether the innings ends normally, after a review overturns a decision (catch), or even mid-declaration (return) — the ground staff still complete the pitch cover-up before the match is truly closed.
There are only two situations where finally does NOT run: if the JVM crashes or is forcibly terminated (e.g. via a power failure), or if the code explicitly calls System.exit() before reaching finally. Aside from these edge cases, finally is guaranteed to execute.
Cricket analogy: The only times cleanup doesn't happen are if the stadium itself collapses (JVM crash) or the match is abruptly called off by the board (System.exit()) — otherwise the ground staff always complete their post-match duties.
Exam trap: if finally itself contains a return statement, it overrides any return value from the try or catch block — this is considered poor practice and can silently swallow exceptions, but it is valid Java and a classic tricky exam question.
4. Example
public class FinallyDemo {
static int test() {
try {
System.out.println("In try");
return 1;
} catch (Exception e) {
System.out.println("In catch");
return 2;
} finally {
System.out.println("In finally — cleanup runs regardless");
}
}
public static void main(String[] args) {
int result = test();
System.out.println("Returned value: " + result);
}
}5. Output
In try
In finally — cleanup runs regardless
Returned value: 16. Key Takeaways
- finally executes after try/catch completes, regardless of whether an exception occurred.
- finally runs even when try or catch contains a return statement — it executes before control actually returns to the caller.
- finally is typically used for cleanup: closing files, connections, or releasing other resources.
- The only ways finally is skipped are a JVM crash or an explicit System.exit() call.
- A return statement inside finally overrides any return value from try/catch — this is legal but bad practice.
Practice what you learned
1. When does the finally block execute?
2. What happens if the try block contains a return statement, and there is also a finally block?
3. Which of the following can prevent a finally block from executing?
4. What happens if a finally block itself contains a return statement?
5. finally is most appropriately used for which purpose?
Was this page helpful?
You May Also Like
try-catch in Java
Learn how to use try, catch, multi-catch, and try-with-resources blocks in Java to handle exceptions cleanly.
Exceptions in Java
Understand what exceptions are in Java, how the Throwable hierarchy is organized, and why exception handling keeps programs robust.
Custom Exceptions in Java
Learn how to define and throw your own checked or unchecked exception classes in Java for clearer, domain-specific error handling.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics