Practice mode Java

In what order are the resources declared in a Java try-with-resources statement closed?

Correct answer C. In the reverse order of their declaration

Explanation

Resources are initialised from left to right and closed in reverse order, so a resource that depends on an earlier one is closed first. Closing happens automatically when the block exits, whether normally or through an exception, and it does not rely on garbage collection. Any exception from close is added as suppressed if another is already propagating.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.