ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct E { };
struct F : E { };
int main() {
    try {
        try { throw F(); }
        catch (E& e) { throw e; }
    }
    catch (F&) { cout << "F"; }
    catch (E&) { cout << "E"; }
    try {
        try { throw F(); }
        catch (E& e) { throw; }
    }
    catch (F&) { cout << "F"; }
    catch (E&) { cout << "E"; }
}
  1. A. FF
  2. B. EE
  3. C. FE
  4. D. EF

Correct answer: D. EF

throw e; throws a NEW object copied from the static type of e, which is E, so the outer F handler cannot match. A bare throw; rethrows the original F object, which the F handler catches.

More OOP with C++ and Java questions

Exams that ask this topic