CatchProvidesOptions
DRAFT

Misconception:

When an exception is thrown in a try block, Java finds the corresponding catch block, and executes only those statements in that block that are necessary to fix the problem.

Incorrect

Only the necessary part of a catch block executes

Correct

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.