✏️ 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
💡

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");
}