Why does order matter when catching exceptions?

Yes he is correct. As you can see in the Javadoc, SQLDataException is a subclass of SQLException. Therefore, your answer is wrong, since it would create a block of unreachable code in the second catch.

In Java, this code would not even compile. In other languages (e.g. python) such code would create a subtle bug, because SQLDataException would actually be caught in the first block, and not in the second one (as would be the case if it was not a subclass).

Had you answered catch(java.sql.SQLException | java.sql.SQLDataException e) { }, it would still be incorrect, since the question asks to handle each exception specifically.

The correct answer is in Grijesh’s answer

Leave a Comment