Exception handling provides a way of transferring control and information from some point in the execution of a program to a handler associated with a point previously passed by the execution (in other words, exception handling transfers control up the call stack)
An exception can be thrown by a throw-expression, dynamic_cast, typeid, new-expression, allocation function, and any of the standard library functions that are specified to throw exceptions to signal certain error conditions (e.g. std::vector::at, std::string::substr, etc).
In order for exception to be caught, the throw-expression has to be inside a try-block or inside a function called from a try-block, and there has to be a catch clause that matches the type of the exception object.
While throw-expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to std::longjmp), its intended usage is error handling.
- Failures to establish invariants
- Failures to meet the postconditions
- Failures to meet the preconditions of the caller
In particular, this implies that the failures of constructors and most operators should be reported by throwing exceptions.
After the error condition is reported by a function, additional guarantees may be provided with regards to the state of the program. The following four levels of exception guarantee are generally recognized, which are strict supersets of each other:
- Nothrow (or nofail) exception guarantee -- the function never throws exceptions. Nothrow (errors are reported by other means or concealed) is expected of destructors and other functions that may be called during stack unwinding. The destructors are noexcept by default. (since C++11) Nofail (the function always succeeds) is expected of swaps, move constructors, and other functions used by those that provide strong exception guarantee.
- Strong exception guarantee -- If the function throws an exception, the state of the program is rolled back to the state just before the function call. (for example, std::vector::push_back)
- Basic exception guarantee -- If the function throws an exception, the program is in a valid state. It may require cleanup, but all invariants are intact.
- No exception guarantee -- If the function throws an exception, the program may not be in a valid state: resource leaks, memory corruption, or other invariant-destroying errors may have occurred.
Generic components may, in addition, offer exception-neutral guarantee: if an exception is thrown from a template parameter (e.g. from the
Compare function object of std::sort or from the constructor of
T in std::make_shared), it is propagated, unchanged, to the caller.
While objects of any complete type and cv pointers to void may be thrown as exception objects, all standard library functions throw anonymous temporary objects by value, and the types of those objects are derived (directly or indirectly) from std::exception. User-defined exceptions usually follow this pattern.
- H. Sutter (2004) "When and How to Use Exceptions" in Dr. Dobb's
- H.Sutter, A. Alexandrescu (2004), "C++ Coding Standards", Item 70
- B. Stroustrup (2000), "The C++ Programming Language"Appendix E"
- H. Sutter (2000) "Exceptional C++"
- D. Abrahams (2001) "Exception Safety in Generic Components"
- D. Abrahams (2001) "Error and Exception Handling"
- M. Cline, C++FAQ Lite 17.11
- S. Meyers (1996) "More Effective C++" Item 13
- M. Cline, C++FAQ Lite 17.12
- H.Sutter, A. Alexandrescu (2004) "C++ Coding Standards" Item 73