ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE

Class A's constructor declares `throws IOException`. Class B extends A. What must every constructor of B do about this checked exception?

  1. A. Checked exceptions can never legally be thrown from a constructor
  2. B. Either also declare `throws IOException` (or a broader checked exception) in its own signature, or avoid it entirely; it cannot be caught around the implicit super() call in the same constructor
  3. C. B cannot be compiled at all while A's constructor throws a checked exception
  4. D. Nothing; subclass constructors are automatically exempt from a superclass constructor's checked exceptions

Correct answer: B. Either also declare `throws IOException` (or a broader checked exception) in its own signature, or avoid it entirely; it cannot be caught around the implicit super() call in the same constructor

Because the super(...) call must be the first statement of B's constructor, it cannot be wrapped in a try/catch there, so B's constructor must itself declare that checked exception (or a supertype of it).

More OOP with C++ and Java questions

Exams that ask this topic