FAQ 26.04 How should a constructor handle a failure?

It should throw an exception.

Some authors suggest that constructors should not throw exceptions. In general this is wrong. For example, constructors cannot return an error code, so attempting to use error codes is inappropriate.

Besides, a failed constructor usually indicates that the object did not achieve a self-consistent state (for example, it may not have been able to allocate sufficient memory, the appropriate file may not have existed, and so on). It is error-prone to let these objects continue to live. For example, if some other member function of the object used a nonexistent resource, it would be an error.

If a constructor experiences an error and the programmer cannot throw an exception, another alternative is to mark the object as a zombie (see FAQ 26.05).



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net