Will the destructor of the base class be called if an object throws an exception in the constructor?
When an exception is thrown from a constructor, the object is not considered instantiated, and therefore its destructor will not be called. But all destructors of already successfully constructed base and member objects of the same master object will be called.
Yes, destructors are guaranteed to be called on stack unwinding, including unwinding due to exception being thrown. There are only few tricks with exceptions that you have to remember: Destructor of the class is not called if exception is thrown in its constructor.
Throwing an exception out of a destructor is dangerous. If another exception is already propagating the application will terminate. This basically boils down to: Anything dangerous (i.e. that could throw an exception) should be done via public methods (not necessarily directly).
No, destructors are called automatically in the reverse order of construction. (Base classes last). Do not call base class destructors.
Yes. The rule is that every object whose constructor has finished successfully will be destructed upon exception. E.g:
class A {
public:
~A() {}
};
class B : public A {
public:
B() { throw 0; }
~B() {}
};
~A() is called. ~B() is not called;
EDIT: moreover, suppose you have members:
struct A {
A(bool t) { if(t) throw 0; }
~A() {}
};
struct B {
A x, y, z;
B() : x(false), y(true), z(false) {}
};
What happens is: x is constructed, y throws, x is destructed (but neither y nor z).
If an exception is thrown during construction, all previously constructed sub-objects will be properly destroyed. The following program proves that the base is definitely destroyed:
struct Base
{
~Base()
{
std::cout << "destroying base\n";
}
};
struct Derived : Base
{
Derived()
{
std::cout << "throwing in derived constructor\n";
throw "ooops...";
}
};
int main()
{
try
{
Derived x;
}
catch (...)
{
throw;
}
}
output:
throwing in derived constructor
destroying base
(Note that the destructor of a native pointer does nothing, that's why we prefer RAII over raw pointers.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With