1Z0-819 · Question #119
Which of the following statements are true about the finally block?
The correct answer is D. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed. F. If an exception is thrown from the finally block, the remaining part of the finally block will not be executed. D is correct because the Java Language Specification guarantees that in a try-with-resources statement, resources are closed (in reverse declaration order) before any associated catch or finally blocks execute - the word "any" is key here, as it correctly signals these blocks…
Question
Options
- AA finally block is required when there are no catch blocks in a try-with-resources statement.
- BMore than one finally block can be declared with a regular try statement.
- CA finally block cannot be declared with a try-with-resources statement.
- DIn a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.
- EA finally block is executed after the resources declared have been closed in a try-with-resources statement.
- FIf an exception is thrown from the finally block, the remaining part of the finally block will not be executed
How the community answered
(52 responses)- A2% (1)
- B6% (3)
- C2% (1)
- D81% (42)
- E10% (5)
Explanation
D is correct because the Java Language Specification guarantees that in a try-with-resources statement, resources are closed (in reverse declaration order) before any associated catch or finally blocks execute - the word "any" is key here, as it correctly signals these blocks are optional but, when present, always run post-closure.
F is correct because a finally block is not immune to its own exceptions: the moment one is thrown inside it, control exits that block immediately, and any remaining statements are skipped - just as in any other code block.
Why the distractors are wrong:
- A - false; try-with-resources requires neither a
catchnor afinallyblock, since automatic resource management is built into the syntax itself. - B - false; a
trystatement can have at most onefinallyblock. - C - false; a
finallyblock is permitted (though optional) with try-with-resources. - E - subtly wrong: it asserts a
finallyblock is executed, implying it's mandatory or guaranteed, butfinallyis optional in try-with-resources and not always present.
Memory tip: Think of try-with-resources as a three-act sequence - close resources → catch → finally - and remember that "finally" means "last to run," not "always exists." If anything throws inside finally, it behaves like any other method: execution stops at the throw.
Topics
Community Discussion
No community discussion yet for this question.