Question 224 of 509
Handling ExceptionseasyMultiple ChoiceObjective-mapped

Quick Answer

The correct answer is that no change is needed; the try-with-resources statement guarantees resource closure even if an exception is thrown. This is because any resource declared in the try-with-resources block, such as a `Connection` which implements `AutoCloseable`, is automatically closed via an implicit `finally` block generated by the compiler. The `close()` method is called regardless of whether the try block completes normally or abruptly due to an exception like `IllegalStateException`, so the database connection will never leak. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this concept tests your understanding of resource management and the `AutoCloseable` interface, often appearing in questions that try to trick you into thinking an explicit `close()` or a separate `finally` block is needed. A common trap is assuming that an exception prevents resource closure, but the try-with-resources mechanism is designed specifically to handle this. Memory tip: think of try-with-resources as having a built-in "safety net" — the resource closes automatically, no matter how the block exits.

1Z0-829 Handling Exceptions Practice Question

This 1Z0-829 practice question tests your understanding of handling exceptions. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. 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 developing a microservice that processes financial transactions. The service reads transaction data from a message queue, validates it, and writes results to a database. The code uses a try-with-resources statement to manage database connections. During testing, you notice that when a transaction fails validation, the service throws an IllegalStateException before closing the database connection, causing a resource leak. You need to ensure that the database connection is always closed properly, even if an exception is thrown during validation. The current code structure is:

try (Connection conn = DriverManager.getConnection(url, user, pass)) { // read from queue // validate transaction // if invalid, throw new IllegalStateException("Invalid transaction"); // write to database

}

Which course of action would you recommend?

Clue words in this question

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

  • 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 1easymultiple 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

No change is needed; the try-with-resources ensures the connection is closed.

Option B is correct because try-with-resources automatically closes any resource that implements AutoCloseable (including Connection) when the try block exits, whether normally or via an exception. The IllegalStateException thrown during validation does not prevent the connection from being closed; the close() method is called as part of the implicit finally block generated by the compiler.

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.

  • Move the connection creation outside the try-with-resources to manually manage it.

    Why it's wrong here

    Manual management is error-prone and defeats the purpose of try-with-resources.

  • No change is needed; the try-with-resources ensures the connection is closed.

    Why this is correct

    try-with-resources automatically closes the resource, even if an exception is thrown.

    Clue confirmation

    The clue word "always" in the question point toward this answer.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Replace the try-with-resources with a try-catch-finally block and close the connection in the finally block.

    Why it's wrong here

    This would work but is more verbose and unnecessary. The try-with-resources is preferred and already handles it.

  • Add a catch block to catch the IllegalStateException and close the connection.

    Why it's wrong here

    The try-with-resources already closes the connection automatically; additional catch is not needed for cleanup.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates mistakenly think an uncaught exception bypasses resource cleanup, but try-with-resources guarantees closure regardless of how the block exits, including exceptions.

Detailed technical explanation

How to think about this question

Under the hood, the Java compiler transforms a try-with-resources statement into a try-finally block that calls close() on each resource, suppressing any exceptions from close() if a primary exception is thrown (via suppressed exceptions). This ensures that even if validation throws an IllegalStateException, the Connection's close() method is invoked, preventing resource leaks in database connection pools or JDBC drivers.

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 practitioner preparing for the 1Z0-829 exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.

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: No change is needed; the try-with-resources ensures the connection is closed. — Option B is correct because try-with-resources automatically closes any resource that implements AutoCloseable (including Connection) when the try block exits, whether normally or via an exception. The IllegalStateException thrown during validation does not prevent the connection from being closed; the close() method is called as part of the implicit finally block generated by the compiler.

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: "always". Absolute qualifier. An answer using 'always' is only correct if there are genuinely no exceptions — absolute statements are often wrong in networking.

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

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.