The RAII idiom binds resources to objects. The object is constructed when the resource is acquired, and the resource is deleted when the object is destroyed. In our case, we are interested only in the second half, the destruction. The advantage of the RAII idiom is that the destructors of all local objects must be called when the control reaches the end of the scope, regardless of how it happens (return, throw, break, and so on). Since we struggled with the cleanup, let's hand that off to an RAII object:
class StorageFinalizer { public: StorageFinalizer(Storage& S) : S_(S) {} ~StorageFinalizer() { S_.finalize(); } private: Storage& S_;};void Database::insert(const Record& r) { S.insert(r); StorageFinalizer ...