ProgrammingVery hardProduct-company placement · C++

What happens in C++11 or later?

What happens in C++11 or later?
struct T { ~T() { throw 1; } };
int main() {
    try { T t; }
    catch (int) { cout << "caught"; }
}
  1. A. caught
  2. B. Nothing is printed and the program ends normally
  3. C. std::terminate is called; caught is not printed
  4. D. Compile-time error: destructors cannot throw

Correct answer: C. std::terminate is called; caught is not printed

Since C++11 destructors are implicitly noexcept unless declared otherwise, so an exception leaving ~T hits the noexcept boundary and std::terminate runs. Declaring ~T() noexcept(false) would let catch see it.

More OOP with C++ and Java questions

Exams that ask this topic