Courseiva
Handling ExceptionseasyMultiple ChoiceObjective-mapped

1Z0-829 Handling Exceptions Practice Question

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?

⚠ Common exam trap

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.

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.

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.

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.

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

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.