Courseiva
Exception Handling and Development ToolshardMultiple ChoiceObjective-mapped

1Z0-811 Exception Handling and Development Tools Practice Question

A developer is using try-with-resources with a custom resource class that implements AutoCloseable. The class's close() method throws a custom exception `ResourceException`. In the try block, an IOException occurs. Which of the following best describes the exception that is propagated to the caller?

⚠ Common exam trap

Watch out — candidates often think the last exception thrown (from close()) overrides the earlier exception, but Java's try-with-resources specifically preserves the try block exception and suppresses the close() exception.

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

The IOException is propagated with the ResourceException added as a suppressed exception.

When using try-with-resources, if both the try block and the close() method throw exceptions, the exception from the try block is the one propagated to the caller, and any exception from close() is added as a suppressed exception to that primary exception. This is specified in Java's try-with-resources semantics (JLS §14.20.3). Therefore, the IOException from the try block is propagated, and the ResourceException from close() is suppressed within it.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The IOException is discarded and the ResourceException is thrown.

    Why it's wrong here

    The close exception is added as suppressed to the IOException; it does not replace it.

  • The IOException is propagated with the ResourceException added as a suppressed exception.

    Why this is correct

    try-with-resources handles this by suppressing the close exception and propagating the primary exception.

  • The IOException is lost and the caller receives no exception.

    Why it's wrong here

    The IOException is the primary exception and will be propagated.

  • Both exceptions are thrown simultaneously as a multi-exception.

    Why it's wrong here

    Java does not support throwing multiple exceptions at once; one is primary, the other suppressed.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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-811 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-811 exam.