As David Abrahams writes, "nobody ever spoke of 'error-safety' before C++ had exceptions."3 The term appeared as the topic of publications in JTC1/SC22/WG21, the C++ standard committee, as early as 1994.4 Exception safety for the C++ standard library was first formalized for STLport by Abrahams, establishing the basic safety/strong safety distinction.5 This was extended to the modern basic/strong/nothrow guarantees in a later proposal.6
Exceptions provide a form of non-local control flow, in that an exception may "bubble up" from a called function. This bubbling can cause an exception safety bug by breaking invariants of a mutable data structure, as follows:7
Code with a bug such as the above can be said to be "exception unsafe".8
The C++ standard library provides several levels of exception safety (in decreasing order of safety):9
Usually, at least basic exception safety is required to write robust code. Higher levels of safety can sometimes be difficult to achieve, and might incur an overhead due to extra copying. A key mechanism for exception safety is a finally clause, or similar exception handling syntax, which ensure that certain code is always run when a block is exited, including by exceptions. Several languages have constructs that simplify this, notably using the dispose pattern, named as using, with, or try-with-resources.
Consider a smart vector type, such as C++'s std::vector or Java's ArrayList. When an item x is added to a vector v, the vector must actually add x to the internal list of objects and update a count field that says how many objects are in v. It may also need to allocate new memory if the existing capacity isn't sufficient.
Exception safety alternatives:
Crichton, Alex (24 July 2015). "Rust RFC: Stabilize catch_panic". The Rust Programming Language. Retrieved 26 May 2022. Code is exception safe if it works correctly even when the functions it calls into throw exceptions. https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md#background-what-is-exception-safety ↩
Lau, Ron (10 November 2020). "Exception safety in JS world". Medium. https://medium.com/@ronlauhk01/exception-safety-in-js-world-7f1e980c409b ↩
Dave Abrahams (2000). Exception-Safety in Generic Components. Generic Programming. Lecture Notes in Computer Science. Vol. 1766. Springer. pp. 69–79. doi:10.1007/3-540-39953-4_6. ISBN 978-3-540-41090-4. Retrieved 2008-08-29. 978-3-540-41090-4 ↩
Colvin, Gregory (1994). "Exception Safe Exceptions" (PDF). C++ Standards Committee Papers. Retrieved 17 December 2021. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1994/N0553.pdf ↩
Abrahams, David. "STLport: Exception Handling". www.stlport.org. Retrieved 17 December 2021. http://www.stlport.org/doc/exception_safety.html ↩
Abrahams, Dave; Colvin, Greg. "Making the C++ Standard Library Exception Safe" (PDF). C++ Standards Committee Papers. Retrieved 17 December 2021. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1086.pdf ↩
Crichton, Alex (24 July 2015). "Rust RFC: Stabilize catch_panic". The Rust Programming Language. Retrieved 26 May 2022. https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md#background-what-is-exception-safety ↩
Bjarne Stroustrup (1997). Appendix E: Standard-Library Exception Safety in "The C++ Programming Language" (PDF) (3rd ed.). Addison-Wesley. ISBN 0-201-88954-4. 0-201-88954-4 ↩
Austern, Matt (30 May 1997). "Standard Library Exception Policy". C++ Standards Committee Papers. Retrieved 26 May 2022. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc ↩