Home / Questions / The finally block is the last bit of code executed before your program ends. True or False? Explain.
Explanatory Question

The finally block is the last bit of code executed before your program ends. True or False? Explain.

👁 40 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

False.
The finally block is not always the last code executed before the program ends.
It executes after the try-catch block completes, regardless of whether an exception was thrown or not.
It is mainly used to release resources (like files or database connections) or perform cleanup tasks.

Example:


try {
    System.out.println("Inside try block");
}
catch (Exception e) {
    System.out.println("Exception caught");
}
finally {
    System.out.println("Finally block executed");
}