Writing clean cleanup logic in C++ without complex try/catch blocks using <scope>:
#include <scope>
void DatabaseTransaction() {
BeginTransaction();
// Automatically rolls back if an exception is thrown!
std::scope_fail rollback([]{ RollbackTransaction(); });
// Automatically releases lock on scope exit (success or failure)
std::scope_exit cleanup([]{ ReleaseLocks(); });
ExecuteQueries();
CommitTransaction();
}std::scope_exit executes upon normal or exceptional scope exit. std::scope_fail executes only if an unhandled exception is currently unwinding the stack!