When an exception propagates out of a function, what happens to its fully constructed automatic objects?
Correct answer
A. Their destructors run in reverse order of construction as the stack unwinds
16 views
A. Their destructors run in reverse order of construction as the stack unwinds
16 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.