Friday, February 24, 2006

Functions inside constructor should throw (if they fail)

This is a for constructors, which do non-trivial resource allocation
and initialization using helper member functions in the class. Exceptions
raised in such member functions should be propagated upto the constructor
and not eat them. In other words, such member functions should throw
exception if they can't satisfy their contract even though they provide
strong exception safety guarantee. Exception propagated upto
the constructor will be automatically propagated where the object is being
constructed indicating failure in object construction. Otherwise, silently
eating exceptions during object initialization will create an object in
an inconsistent state. The scope in which it is created will never know
that actually the object construction failed. If object construction can
be reasonably performed even in the face of exception, then it need not be
propagated out.

Example, LStack: A Stack implemented as a linked-list.

LStack::LStack (const LStack & s)
{
this->copy_all_nodes (s);
}

/// Following function provides strong exception safety guarantee
LStack::copy_all_nodes (const LStack &s)
{
try {
/// Copy nodes from the linked list of s into this's linked-list
/// copying of list elements may fail at arbitrary intermediate
/// position.

}
catch (...) {
this->delete_partially_initialized_list ();
/// throw; /// Should be done.
}
}

In the above case, copy_all_nodes is flowed because it failed to satisfy its
contract and still did not indicate its failure to the constructor.
Consequently, the client trying to use copy of LStack object will
be disappointed for sure.

This is important in case of constructor because it is difficult to tell
that object construction failed because there is no return value.

3 comments:

Dissertation Help said...

Wow! this blog is good. it is really excellent c++ blog. I like this very much. I am learning programming and hope your blog can help me.

Medical assistant schools said...

I recently came across your blog and have been reading along. I think I will leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Buy Dissertation said...

I know very much about C++ and is in good practices but never heard this one. It is new to me.