CatchProvidesOptions
DRAFT
Observed

CatchProvidesOptions
Incorrect

Only the part of a catch block necessary to fix the cause of an exception is executed

Correct

When an exception is caught, all statements in the corresponding catch block execute

Correction
Here is what's right.

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).

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.