The
C++ standard library provides several levels of exception safety (in decreasing order of safety): •
Basic exception safety: Partial execution of failed operations can result in side effects, but all
invariants are preserved. Any stored data will contain valid values which may differ from the original values.
Resource leaks (including
memory leaks) are commonly ruled out by an invariant stating that all resources are accounted for and managed. •
No exception safety: No guarantees are made. 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. == Example ==