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.
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.
Comments