Question 257 of 509
Handling ExceptionshardMultiple ChoiceObjective-mapped

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.”

1Z0-829 Handling Exceptions Practice Question

This 1Z0-829 practice question tests your understanding of handling exceptions. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

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?

Clue words in this question

Noticing these words before you look at the options changes how you read each choice.

  • Clue: "best"

    Why it matters: Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.

  • Clue: "always"

    Why it matters: Absolute qualifier. An answer using 'always' is only correct if there are genuinely no exceptions — absolute statements are often wrong in networking.

Question 1hardmultiple choice
Full question →

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) { ... }

Option D is correct because 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.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

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.

    Clue confirmation

    The clue words "best", "always" in the question point toward this answer.

    Related concept

    Read the scenario before looking for a memorised answer.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates may 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.

Detailed technical explanation

How to think about this question

The try-with-resources statement, introduced in Java 7, works by calling the close() method of any resource that implements AutoCloseable in the reverse order of their declaration. Under the hood, the compiler generates a hidden finally block that catches exceptions from close() and adds them as suppressed exceptions to any primary exception thrown in the try block, preserving the original failure context. In real-world scenarios, such as file uploads or database connections, a failure during close() (e.g., network timeout) should not mask the original upload failure, and try-with-resources handles this correctly by using suppressed exceptions.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related 1Z0-829 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free 1Z0-829 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this 1Z0-829 question test?

Handling Exceptions — This question tests Handling Exceptions — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Rewrite the code to use try-with-resources: try (UploadSession session = new UploadSession()) { ... } catch (UploadException e) { ... } — Option D is correct because 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.

What should I do if I get this 1Z0-829 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Are there clue words in this question I should notice?

Yes — watch for: "best", "always". Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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 →

How Courseiva writes practice questions · Editorial policy

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: Option A is correct because 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: Option A is correct because 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: Option C is correct because 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: Option B is correct because 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

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.

Loading comments…

Sign in to join the discussion.

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.