The correct way to do this in C++ is to invoke some form of RAII whereby the failure logic happens automatically in an object's destructor.
class CleanupObject
{
public:
CleanupObject() : successState( false )
{
}
void completedOk()
{
successState = false;
}
~CleanupObject()
{
if( successState )
normalCleanup();
else
failedCleanup();
private:
bool successState;
};
In your code, add something like:
void doStuff()
{
CleanupObject obj;
if( operation.fails() )
return;
obj.completedOk(); }
Your cleanup code must not throw any exceptions. Note here that your operations could throw, and your cleanup would still occur. (Your caller would need to catch.)
Using boost's "Scope Exit" might make it easier still to implement "exit code" that must be run at the end of a scope.