December 2003
Generic<Programming>: Exception Safety Analysis
Andrei Alexandrescu and David B. Held, p 40
Programming is understanding and exceptions are no exception
(wacky guarantee)
- basic guarantee
- strong guarantee
- nothrow guarantee
void Fun() {
Gun();
Hun();
}
Gun and Hum strong. What about Fun? May be from strong to wacky...
But if Gun
is strong and pure, then Fun stays strong.
Conclusion
- In a sequence of function calls, you obtain the strong guarantee
if you write your code as a sequence of pure calls, followed by
no more than one strong call, followed by a sequence of nothrow calls.
- Purity is context dependent; you can transform a strong function
into a pure function by having it operate on local automatic
data. Note that pure functions are always strong.
- Alternatively, you can use
try/catch
with purity
restoration in the catch
clause. A variant of that
uses ScopeGuard for automatically restoring purity in case of an
exception.
- When the system has broken invariants, always restore them with
nothrow calls.
- To determine a function's exception-safety behavior, analyze it
with the algorithm in the previous section, having at hand the
summaries of the functions it invokes.
Conversations: Reusing Streams
by Herb Sutter and Jim Hyslop, p 52
Wisdom from the anointed one on closing and opening streams
Web version,
C/C++ Users Journal ToC
Marc Girod
Last modified: Fri Nov 28 16:27:48 EET 2003