1Z0-829 Handling Exceptions Practice Question
Exhibit
Refer to the exhibit.
try (FileInputStream fis = new FileInputStream("file.txt");
BufferedInputStream bis = new BufferedInputStream(fis)) {
// read data
} catch (IOException e) {
System.out.println(e.getMessage());
Throwable[] suppressed = e.getSuppressed();
}In the following try-with-resources block, resources are declared as: try (FileInputStream fis = new FileInputStream("data.txt"); BufferedInputStream bis = new BufferedInputStream(fis)) { ... } If an IOException occurs during the closing of both resources, which resource's close exception is returned by e.getSuppressed()?
⚠ Common exam trap
The trap is that candidates may think the suppressed array contains both exceptions or that it contains the exception from the first resource closed. Actually, the first closed resource's exception is primary, and the second's is suppressed.
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
✓
Only the exception from FileInputStream close is in the suppressed array.
In a try-with-resources statement, resources are closed in the reverse order of their declaration. Here, FileInputStream is declared first and BufferedInputStream second, so BufferedInputStream is closed first. If both resources throw an IOException during closing, the exception from BufferedInputStream (the first to close) becomes the primary exception, and the exception from FileInputStream (the second to close) is suppressed. The primary exception is thrown from the try block, while the suppressed exception is accessible via the getSuppressed() method. Therefore, e.getSuppressed() returns an array containing the exception from FileInputStream. Option B is correct because only the FileInputStream's close exception is in the suppressed array.
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 suppressed array contains both exceptions.
Why it's wrong here
Incorrect: The suppressed array contains only the exception from the second-closed resource, not both.
- ✓
Only the exception from FileInputStream close is in the suppressed array.
Why this is correct
Correct: Since BufferedInputStream closes first and its exception becomes primary, the suppressed exception from FileInputStream is the only one in the suppressed array.
- ✗
The suppressed array contains the exception from FileInputStream.
Why it's wrong here
Partially correct, but option B specifies 'only', which is more accurate given that only one resource's closure exception is suppressed.
- ✗
Only the exception from BufferedInputStream close is in the suppressed array.
Why it's wrong here
Incorrect: The exception from BufferedInputStream is the primary exception, not suppressed.
Go deeper
Related to this question
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 →
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.