DRAFT
Observed
Only the part of a catch block necessary to fix the cause of an exception is executed
When an exception is caught, all statements in the corresponding catch block execute
Take the following example:
int moveTo(final Position p) {
moved = true;
try {
x = p.x;
y = p.y;
} catch (final NullPointerException ex) {
x = 5;
y = 8;
}
return ++steps;
}
If p==null
, then x=p.x
throws a NullPointerException
, which is caught by the catch
block. Now the entire catch block executes (not only the statement fixing x
by assigning it a value x=5
).
Follow us on twitter to hear about new misconceptions.