Courseiva
Controlling Program FlowhardMultiple ChoiceObjective-mapped

1Z0-829 Controlling Program Flow Practice Question

A resource is declared in a try-with-resources statement. The try block throws an exception. The close method throws a different exception. What exception is thrown by the try-with-resources statement?

⚠ Common exam trap

Many exam-takers assume the last exception thrown (from close()) overrides the earlier one, or that both exceptions are thrown simultaneously, but Java's suppressed exception mechanism ensures the try block's exception is primary and close() exceptions are silently attached.

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 exception from the try block.

In a try-with-resources statement, if both the try block and the close() method throw exceptions, the exception from the try block is the one thrown by the statement. The exception from close() is suppressed and added to the thrown exception's suppressed exception list (via Throwable.addSuppressed()). This is specified by JLS §14.20.3.2 and ensures the primary failure (the try block exception) is not masked.

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 exception from the close method.

    Why it's wrong here

    The close exception is added to the suppressed list, not thrown.

  • The exception from the try block.

    Why this is correct

    The try block exception is the primary exception; the close exception is suppressed.

  • A compilation error occurs.

    Why it's wrong here

    This code is valid.

  • Both exceptions are thrown simultaneously.

    Why it's wrong here

    Only one exception is thrown; the other is suppressed.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.