J.V · Java 25 practice question
What happens when this Java 25 program runs?
void main() {
try {
int x = 10;
} finally {
throw new IllegalStateException("cleanup");
}
}
Check answer and explanation
Correct answer: The try statement completes abruptly by throwing IllegalStateException
Explanation
When the try block completes normally, the finally block executes; because the finally block completes abruptly by throwing IllegalStateException, the entire try statement completes abruptly for that same reason.
Common trap
Normal completion of the try block does not override an abrupt completion in the finally block. A finally block is legally allowed to throw exceptions.
Source: JLS SE 25 §14.20.2. Execution of try - finally and try - catch - finally
Checked against the cited text on 2026-11-25.