Module: | Classes, Objects, OOPs & Inheritance
Q45: Consider the following statements regarding the execution flow of the finally block:
1. The finally block is fundamentally guaranteed to execute after the try and catch blocks finish, making it the architecturally correct location for safely closing database connections and file streams.
2. If a return statement is encountered directly inside the try block, the Java Virtual Machine will immediately exit the method, entirely bypassing the execution of the finally block.
3. The execution of a finally block can be permanently aborted if the application explicitly invokes System.exit(0) or if the underlying host machine experiences a catastrophic power failure.
Which of the above statements is/are correct?
2. If a return statement is encountered directly inside the try block, the Java Virtual Machine will immediately exit the method, entirely bypassing the execution of the finally block.
3. The execution of a finally block can be permanently aborted if the application explicitly invokes System.exit(0) or if the underlying host machine experiences a catastrophic power failure.
Which of the above statements is/are correct?
✅ Correct Answer: B
🎯 Quick Answer:
The correct combination is 1 and 3. Statement 2 is incorrect because the JVM recognizes the finally block's absolute priority. If a return statement is hit inside a try or catch block, the JVM securely caches the return value, executes the entire finally block, and only then executes the actual return instruction to exit the method.Structural Breakdown: The structure is try {} catch {} finally {}. The catch block is technically optional if a finally block is present (try-finally is valid). Historical/Related Context: In systems programming, failing to release memory or file handles causes resource leaks that eventually crash servers.
The finally block was Java's original mechanism to ensure that no matter how complex the branching logic or exception flow became, the critical cleanup instruction was inescapable.
Causal Reasoning: The System.exit() command (Statement 3) circumvents the finally block because System.exit() does not simply throw an error; it sends an immediate, non-negotiable termination signal directly to the Java Virtual Machine process at the Operating System level, destroying the entire execution environment before the finally block can be read.