A finally block guarantees that cleanup code runs whether the try block completes normally or throws an exception, which makes it the natural place for releasing resources like streams, connections, or locks that must never be left open.
When reading a file manually with a FileInputStream, wrapping the read logic in try and closing the stream in finally ensures the file handle is released even if an IOException interrupts the read partway through.
FileInputStream in = null;
try {
in = new FileInputStream("data.txt");
// read data
} catch (IOException e) {
// handle error
} finally {
if (in != null) {
in.close();
}
}A concise interview answer is
Yes — I've used finally most often for resource cleanup, like closing a file stream or database connection, since it's guaranteed to run whether or not an exception occurs. These days I'd typically prefer try-with-resources for anything AutoCloseable, but finally is still useful for cleanup that doesn't fit that pattern, like releasing a manually acquired lock.
Last updated: 11 November 2026
Previous question: Give an example of when you would deliberately use a checked exception instead of an unchecked one.
Next question: Can a finally block cause unexpected behaviour or side effects?