Card 01/ 10
All 10 cards
ConceptDifficulty: Beginner1 min
What Happens the Moment Something Goes Wrong
A method is halfway through writing a report when the disk fills. It cannot finish, and it cannot decide what should happen instead — the method that asked for the report knows that, and it is three calls away.
An exception is how a method says "I cannot complete this" and hands the decision upwards. Execution of the current method stops immediately, and Java starts looking for somebody who said they would handle it.
try {
writeReport(path);
System.out.println("written");
} catch (IOException e) {
System.out.println("could not write: " + e.getMessage());
}If writeReport throws, line 3 never runs. Control jumps straight to the matching catch, which is the only place execution resumes.
| Block | Runs |
|---|---|
try | Until it finishes or something throws |
catch | Only if something threw, and only for a matching type |
finally | Always, whichever of the two happened |
So an exception is a return path that skips every line between the failure and the handler. That is exactly what makes it useful — no intermediate method has to check a return code — and exactly what makes swallowing one so expensive.