Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if an exception is raised in constructor [duplicate]

Tags:

c++

Possible Duplicate:
What happens to base class destructor if a derived class destructor throws an exception

If I throw an exception in the constructor of a derived class, is the destructor called to clean up the base class?

like image 613
userbb Avatar asked Apr 28 '11 09:04

userbb


People also ask

What happens if exception occurs in constructor?

When throwing an exception in a constructor, the memory for the object itself has already been allocated by the time the constructor is called. So, the compiler will automatically deallocate the memory occupied by the object after the exception is thrown.

Can a copy constructor throw an exception?

However, the copy constructor for an exception object still must not throw an exception because compilers are not required to elide the copy constructor call in all situations, and common implementations of std::exception_ptr will call a copy constructor even if it can be elided from a throw expression.

What happens when a constructor throws an exception Java?

Throwing an exception in a constructor can lead to partially initialized objects. As described in Guideline 7.3 of Java Secure Coding Guidelines, partially initialized objects of a non-final class are prone to a security concern known as a Finalizer Attack.

What happens if an exception is thrown from an object's constructor and object's destructor?

When an exception is thrown, destructors of the objects (whose scope ends with the try block) are automatically called before the catch block gets executed. That is why the above program prints “Destructing an object of Test” before “Caught 10“.


3 Answers

The destructors of the base class(es) are then called in the reverse order.

Before that, the destructors of any already initialized member will also get called.

However, the destructor of the currently constructed derived-class won't get called, as this part of the object will not have been really constructed yet.

like image 97
ereOn Avatar answered Oct 14 '22 22:10

ereOn


Yes. For example, consider:

#include <iostream>

class B
{
public:
    B() { std::cout << "B()\n"; }
    ~B() { std::cout << "~B()\n"; }
};

class D : public B
{
public:
    D() { std::cout << "D()\n"; throw 23; }
    ~D() { std::cout << "~D()\n"; }
};

int main()
try
{
    D d;
    return 0;
}
catch(...) {}

Output:

B()
D()
~B()
like image 33
Stuart Golodetz Avatar answered Oct 14 '22 23:10

Stuart Golodetz


http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.10

Every data member inside your object should clean up its own mess.

If a constructor throws an exception, the object's destructor is not run. If your object has already done something that needs to be undone (such as allocating some memory, opening a file, or locking a semaphore), this "stuff that needs to be undone" must be remembered by a data member inside the object.

Base destructors are guaranteed to be called, but not the destructor of the object itself.

like image 7
log0 Avatar answered Oct 14 '22 22:10

log0