ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

Which line fails to compile?

Which line fails to compile?
void run() {
    try { int x = 1; }
    catch (RuntimeException e) { }      // line 1
    try { int y = 2; }
    catch (Exception e) { }             // line 2
    try { int z = 3; }
    catch (java.io.IOException e) { }   // line 3
}
  1. A. Line 1
  2. B. Line 2
  3. C. Line 3
  4. D. None of them

Correct answer: C. Line 3

It is a compile-time error to catch a checked exception that the try block cannot throw, so catching IOException here is rejected. Unchecked exceptions and Exception itself (which covers unchecked ones) may always be caught.

More OOP with C++ and Java questions

Exams that ask this topic