When an exception is thrown and control passes from the try block to the handler, C ++ runtime calls destructors for all automatic objects built from the beginning of the try block. This process is called stack expansion. Automatic objects are destroyed in the reverse order of their design. (Automatic objects are local objects that were declared automatically or registered or not declared as static or extern. An automatic object x is deleted whenever the program exits the block in which x is declared.)
If an exception is thrown when constructing an object consisting of subobjects or array elements, destructors are called only for those subobjects or array elements that were successfully created before the exception was thrown. The destructor for the local static object will be called only if the object was successfully constructed.
If during the unwinding of packets the destructor throws an exception and the exception is not processed, the terminate () function is called.
Example: see disassembly below. You will see that the destructor is already pushed onto the stack.
class Test { public: Test() { std::cout<<"C'tor\n"; } ~Test() { std::cout<<"D'tor\n"; } } int main()
Satbir
source share