Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly handle exceptions in constructors? [duplicate]

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.

like image 559
Johny_sa Avatar asked Oct 04 '12 09:10

Johny_sa


People also ask

How do you handle exceptions that arise in a 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.

Is it OK to throw exception in constructor in C++?

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.

Can a constructor throws an exception How do you handle the error when the constructor fails?

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.


2 Answers

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.

like image 67
James Kanze Avatar answered Sep 28 '22 19:09

James Kanze


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.

like image 31
Dietmar Kühl Avatar answered Sep 28 '22 19:09

Dietmar Kühl