1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A destructor throws while the stack is already unwinding…

Added 30 Nov -0001

A destructor throws while the stack is already unwinding from another exception. What is the outcome in C++11 and later?

  1. Both exceptions are merged into a std::nested_exception and rethrown together
  2. The program continues normally, with the destructor's exception silently discarded
  3. std::terminate is called, because two active exceptions cannot propagate at once Correct answer
  4. The new exception replaces the old one and unwinding continues from that point

Correct answer

C. std::terminate is called, because two active exceptions cannot propagate at once

13 views

MCQSEXAM

Explanation

Destructors are implicitly noexcept, so an exception escaping one during unwinding calls std::terminate instead of propagating. Even with no exception in flight, throwing from a destructor is hazardous because cleanup often runs on partially destroyed objects. Destructors should catch and log, or expose an explicit close() that can report failure.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs