ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE

java try { throw new ArithmeticException("x"); } catch (RuntimeException e) { System.out.print("R"); } catch (ArithmeticException e) { System.out.print("A")…

try {
    throw new ArithmeticException("x");
} catch (RuntimeException e) {
    System.out.print("R");
} catch (ArithmeticException e) {
    System.out.print("A");
}
What happens?
  1. A. Prints "RA"
  2. B. Compile error: the ArithmeticException catch block is unreachable, since RuntimeException (its superclass) already catches it first
  3. C. Prints "R"
  4. D. Prints "A"

Correct answer: B. Compile error: the ArithmeticException catch block is unreachable, since RuntimeException (its superclass) already catches it first

Java requires catch clauses to be ordered from most specific to least specific; putting the broader RuntimeException before its subclass ArithmeticException makes the second clause unreachable, which javac rejects.

More OOP with C++ and Java questions

Exams that ask this topic