ProgrammingModerateProduct-company placement · Java

What happens when this code is compiled?

What happens when this code is compiled?
try { new java.io.FileReader("x.txt"); }
catch (Exception e)            { System.out.print("E"); }
catch (java.io.IOException e)  { System.out.print("IO"); }
  1. A. Compile-time error: the IOException catch is unreachable
  2. B. It compiles and prints E if the file is missing
  3. C. It compiles and prints IO if the file is missing
  4. D. It compiles and prints EIO if the file is missing

Correct answer: A. Compile-time error: the IOException catch is unreachable

Catch clauses are tried top to bottom, so a subclass catch placed after its superclass can never run; Java makes that a compile-time error. The more specific catch must come first.

More OOP with C++ and Java questions

Exams that ask this topic