Possible Duplicate:
How to clean initialized resources if exception thrown from constructor in c++
How do I handle exception in constructors if I am creating 6 objects and those objects create 5 object and fails while creating the 6th one?
Thanks.
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.
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.
If your construction fails and throws an exception, ~Mutex() will not be called and mutex_ will not be cleaned up. Don't throw exceptions in constructors.
Yes, constructors are allowed to throw an exception in Java. A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class.
The usual behavior is just to let the exception propagate. Destructors for any completely constructed base classes and members will be called; if the first five objects are members, they will be correctly destructed.
The only case where a problem might occur is if the objects you are
talking about have been dynamically allocated (using new
). If that is
the case: the first thing to ask yourself is why? Why are you
dynamically allocating, and not making the object a concrete member? In
my experience, such a need is very, very rare, except in a few special
cases (e.g. the compilation firewall idiom), in which case, there will
normally be exactly one object in the class (e.g. a pointer to the
implementation object). In such cases, there is no problem, because if
the new
of that object fails, nothing else has been done which needs
undoing.
If you find yourself in the exceptionally rare case where you really do have to use dynamic allocation and have more than one such object (e.g. because you have two subobjects which are polymorphic), then you'll have to ensure that each of the allocations is wrapped in some sort of a sub-object (a smart pointer will do the trick); once the first sub-object has been successfully constructed, its destructor will be called if the constructor fails at some later point.
When an exception is thrown in the constructor all fully constructed subobjects are destroyed. Since it is good practice to have destructors of constructed objects look after their resoyrces, nothing needs to be done for these subobjects. What remains is cleaning up in the body of the constructor currently executing when the exception is thrown. However, this is no different to the clean-up in any other function.
Note that the destruction order is the reverse of the construction. That is, clean-up in the body starts first when all subobjects are not, yet, destroyed. Then the members are destroyed, then non-virtual base classes, and finally virtual base classes.
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