The code does not compile because methodA does not declare that it throws Exception. This compilation error arises from Java’s improved rethrow analysis, which only permits a catch block to catch a broader checked exception type if that exception is explicitly listed in the enclosing method’s throws clause. Here, methodA throws IOException but does not declare Exception, and the catch block catches Exception—a broader type—so the compiler rejects the code. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this tests your understanding of precise rethrow and multi-catch rules, a common trap where candidates assume catching a supertype is always valid. Remember: improved rethrow analysis requires the exception parameter to be effectively final and the caught exception to be declared in the method signature. A handy memory tip is “catch what you throw, not what you wish”—the method’s throws clause must match the caught exception’s type for the compiler to accept it.
1Z0-829 Handling Exceptions Practice Question
This 1Z0-829 practice question tests your understanding of handling exceptions. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. 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.
Exhibit
Refer to the exhibit.
public class ExceptionDemo {
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println("Caught in main: " + e.getClass().getSimpleName());
}
}
static void methodA() throws IOException {
try {
throw new IOException();
} catch (Exception e) {
throw e;
}
}
}
What is the result of executing the code in the exhibit?
Refer to the exhibit.
public class ExceptionDemo {
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println("Caught in main: " + e.getClass().getSimpleName());
}
}
static void methodA() throws IOException {
try {
throw new IOException();
} catch (Exception e) {
throw e;
}
}
}
A
The code compiles and prints 'Caught in main: IOException' because the compiler uses improved rethrow analysis.
Why wrong: Improved analysis works only if the catch parameter is final and the exception type is effectively final; but here the catch type is Exception not IOException.
B
Prints: Caught in main: Exception
Why wrong: Code does not compile.
C
The code does not compile because methodA does not declare that it throws Exception.
The catch parameter is Exception, so rethrow is considered as throwing Exception, which is not declared.
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The code does not compile because methodA does not declare that it throws Exception.
The code does not compile because methodA explicitly throws an IOException, but the catch block in main catches Exception (a broader type). However, the compiler performs improved rethrow analysis only when the exception parameter is effectively final and the catch block is multi-catch or rethrows a checked exception that is declared in the method signature. Here, methodA does not declare that it throws Exception, and the catch block catches Exception, which is not a declared exception in methodA's throws clause, causing a compilation error.
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.
✗
The code compiles and prints 'Caught in main: IOException' because the compiler uses improved rethrow analysis.
Why it's wrong here
Improved analysis works only if the catch parameter is final and the exception type is effectively final; but here the catch type is Exception not IOException.
✗
Prints: Caught in main: Exception
Why it's wrong here
Code does not compile.
✓
The code does not compile because methodA does not declare that it throws Exception.
Why this is correct
The catch parameter is Exception, so rethrow is considered as throwing Exception, which is not declared.
Related concept
Read the scenario before looking for a memorised answer.
✗
Prints: Caught in main: IOException
Why it's wrong here
Code does not compile.
Common exam traps
Common exam trap: answer the scenario, not the keyword
The trap here is that candidates assume improved rethrow analysis allows catching a broader exception type (Exception) without the method declaring it, forgetting that the method's throws clause must still declare the caught exception or the catch block must match the thrown exception exactly.
Detailed technical explanation
How to think about this question
In Java 7+, improved rethrow analysis allows a catch block to throw a more precise exception type if the parameter is effectively final and the method declares the exception. However, this analysis only works when the catch block catches a supertype (e.g., Exception) and the method declares the specific subtypes (e.g., IOException). If the method does not declare the caught exception type, the compiler requires the catch block to match exactly or the method to declare the broader exception. Here, methodA throws IOException but does not declare Exception, so the catch block catching Exception is invalid unless main declares Exception, which it does not.
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 small business has 20 workstations on the 192.168.1.0/24 network and one public IP from its ISP. The router uses PAT (NAT overload) so all 20 devices share one public address using different source ports. NAT questions test whether you understand the four address terms and which direction each translation applies.
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.
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: The code does not compile because methodA does not declare that it throws Exception. — The code does not compile because methodA explicitly throws an IOException, but the catch block in main catches Exception (a broader type). However, the compiler performs improved rethrow analysis only when the exception parameter is effectively final and the catch block is multi-catch or rethrows a checked exception that is declared in the method signature. Here, methodA does not declare that it throws Exception, and the catch block catches Exception, which is not a declared exception in methodA's throws clause, causing a compilation error.
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.
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 →
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.
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.
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.
Sign in to join the discussion.