1Z0-829 Handling Exceptions Practice Question
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?
⚠ Common exam trap
It's easy for candidates to 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.
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 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.
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.
- ✗
Prints: Caught in main: IOException
Why it's wrong here
Code does not compile.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 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 →
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.