Practice mode Java

What happens if a try statement places catch (Exception e) before catch (IOException e)?

Correct answer C. The code fails to compile

Explanation

Catch clauses are tested in order, and a clause for Exception already covers every IOException, so the later clause could never run. The language makes this a compile-time error rather than a warning. Always list more specific exception types before their superclasses, so that each handler can be reached.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.