Item 39. Exception Safe Functions

The hard part about writing exception safe code isn’t the throwing or catching of exceptions; it’s everything in between. As a thrown exception wends its way from the throw expression to the catch clause, every partially executed function on that path must “clean up” any important resources that it controls before its activation record is popped off the execution stack. Generally (but not always), all that is required to write an exception safe function is a moment’s reflection and some common sense.

For example, consider the implementation of String assignment from Assignment and Initialization Are Different [12, 41]:

The implementation of this function may look superfluously ornate, since we could have coded ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.