1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. When an exception propagates out of a function, what…

Added 30 Nov -0001

When an exception propagates out of a function, what happens to its fully constructed automatic objects?

  1. Their destructors run in reverse order of construction as the stack unwinds Correct answer
  2. They are leaked, because destructors only run at normal return
  3. They are destroyed only if their class has a virtual destructor
  4. std::terminate is called before any destructor runs

Correct answer

A. Their destructors run in reverse order of construction as the stack unwinds

16 views

MCQSEXAM

Explanation

Stack unwinding destroys automatic objects in reverse order of construction, which is what makes RAII reliable for locks and file handles during exceptions. Objects whose construction never completed are not destroyed, so a constructor acquiring two resources should release the first itself if the second acquisition throws. A destructor that throws during unwinding terminates the program.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? 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