ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct M { M() { throw 1; } };
struct S {
    M m;
    S() try : m() { }
    catch (int) { cout << "S"; }
};
int main() {
    try { S s; } catch (int) { cout << "main"; }
}
  1. A. S
  2. B. Smain
  3. C. main
  4. D. Compile-time error: try cannot wrap an initializer list

Correct answer: B. Smain

A function-try-block can catch exceptions from member initialisers, but for a constructor the handler cannot swallow them: reaching its end automatically rethrows, because the object could not be constructed.

More OOP with C++ and Java questions

Exams that ask this topic