Question 262 of 513
Using Try-With-Resources to Automatically Close Resources with Checked Exceptions
You are responsible for a Java 17 application that processes user uploads. The application uses a custom AutoCloseable resource, UploadSession, which must always be closed after use to free server resources. A junior developer wrote the following code:
``` UploadSession session = new UploadSession(); try { session.upload(data); // more processing
} catch (UploadException e) {log.error("Upload failed", e);
} finally {session.close();
}
```
During a code review, you notice that the close() method of UploadSession throws a checked CloseException. The current code does not handle this exception. Which course of action should you recommend to ensure the application is robust and follows best practices?
Quick Answer
The correct choice is to rewrite the code using try-with-resources, as this construct automatically handles resource closure even when the close() method throws a checked exception. When you declare the resource in the try-with-resources statement, the Java compiler generates an implicit finally block that calls close(), and any checked exception from close() is either caught by an accompanying catch block or must be declared in the enclosing method’s throws clause. This approach is more robust than manual try-finally because it eliminates the risk of forgetting to close the resource and properly integrates the checked exception from close into the exception handling flow. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this concept frequently appears in questions about resource management and exception handling, often testing your ability to recognize that try-with-resources can handle checked exceptions from close without requiring a nested try-catch. A common trap is assuming you still need an explicit finally block, but the whole point of try-with-resources is to make that redundant. Memory tip: “Try-with-resources closes automatically, even when close throws a checked exception.”
⚠ Common exam trap
A common mix-up: candidates think a finally block is always necessary for cleanup, but try-with-resources automatically handles resource closure and exception suppression, making it the preferred pattern for AutoCloseable resources.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Rewrite the code to use try-with-resources: try (UploadSession session = new UploadSession()) { ... } catch (UploadException e) { ... }
Try-with-resources automatically calls the close() method on the UploadSession resource when the try block exits, whether normally or abruptly. This eliminates the need for an explicit finally block and ensures that any checked exception thrown by close() is either caught or propagated according to the developer's declared exception handling, making the code both robust and concise.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Add a try-catch block inside the finally block to handle CloseException from session.close().
Why it's wrong here
Verbose and does not use the automatic suppression mechanism.
- ✗
Declare the method with throws CloseException, and remove the finally block.
Why it's wrong here
Pushes the problem to the caller without guaranteeing closure on all paths.
- ✗
Keep the code as-is. The CloseException is not critical; it can be ignored.
Why it's wrong here
Ignoring exceptions is bad practice and can cause resource leaks.
- ✓
Rewrite the code to use try-with-resources: try (UploadSession session = new UploadSession()) { ... } catch (UploadException e) { ... }
Why this is correct
Try-with-resources handles closure automatically and properly suppresses exceptions.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
5 more ways this is tested on 1Z0-829
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which TWO statements about the try-with-resources statement are correct? (Choose two.)
hard- ✓ A.The resource class must implement AutoCloseable.
- B.The resource variable must be declared as final.
- ✓ C.Resources are closed in the reverse order of their declaration.
- D.A try-with-resources statement cannot have a finally block.
- E.Resources are closed before the try block executes.
Why A: The try-with-resources statement requires that each resource implements the AutoCloseable interface (or its subinterface Closeable). This interface defines the close() method, which is automatically invoked by the JVM when the try block completes, ensuring proper resource management without explicit finally blocks.
Variation 2. Which TWO statements about try-with-resources are correct? (Choose two.)
medium- ✓ A.Resources are closed in the reverse order of their declaration.
- B.Resources are closed in the same order as their declaration.
- C.Resources declared in try-with-resources must implement the Closeable interface.
- D.Resources declared earlier in the try clause are closed first.
- ✓ E.If an exception is thrown from the try block and another from a resource's close() method, the try block exception propagates and the close() exception is added as a suppressed exception.
Why A: The Java Language Specification (JLS §14.20.3) mandates that resources declared in a try-with-resources statement are closed in the reverse order of their declaration. This ensures that if a resource depends on another declared earlier, the dependent resource is closed first, preventing resource leaks or inconsistent states.
Variation 3. Which TWO statements about the try-with-resources statement are true? (Choose two.)
easy- A.The resources must implement the Closeable interface.
- B.Resources can be reassigned inside the try block.
- ✓ C.Resources declared in the try-with-resources block are closed automatically.
- ✓ D.Resources are closed in the reverse order of their declaration.
- E.Multiple resources are separated by commas.
Why C: The try-with-resources statement automatically closes each resource declared in the resource specification after the try block completes, whether normally or abruptly. This is the primary purpose of the construct: to ensure reliable resource cleanup without requiring an explicit finally block.
Variation 4. Which TWO statements about try-with-resources are true? (Choose two.)
medium- A.Resources are closed in the order they are declared.
- ✓ B.If both the try block and the close method throw exceptions, the close exception is suppressed.
- C.Only a single resource can be managed in a try-with-resources statement.
- ✓ D.Resources must implement AutoCloseable.
- E.Resources are closed after the catch block if an exception occurs.
Why B: When both the try block and the close method throw exceptions, the exception from the close method is suppressed and added to the suppressed exceptions list of the exception thrown by the try block. This behavior is defined by the try-with-resources specification in Java, ensuring that the primary exception is not masked by resource-closing exceptions.
Variation 5. A Java application uses a custom resource class implementing AutoCloseable. The close() method throws a checked exception. In a try-with-resources block, if both the try block and the close() method throw exceptions, which exception is propagated to the caller? Assume the resource is declared in try-with-resources.
medium- ✓ A.The exception from the try block is the primary exception, and the close() exception is added as a suppressed exception.
- B.The exception from close() is always the primary exception.
- C.Both exceptions are thrown simultaneously as a multi-catch exception.
- D.The exception from the try block is always suppressed.
Why A: In a try-with-resources block, when both the try block and the close() method throw exceptions, the exception from the try block is the primary exception propagated to the caller. The exception thrown by close() is added to the primary exception's suppressed exception list, as defined by the Java Language Specification (JLS §14.20.3.2). This ensures that the most relevant exception (the one from the application logic) is not masked by a resource-closing failure.
Last reviewed: Jun 11, 2026
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.