Is this a good way to implement a Finally-like behavior in standard C++? (Without special pointers)
class Exception : public Exception
{ public: virtual bool isException() { return true; } };
class NoException : public Exception
{ public: bool isException() { return false; } };
Object *myObject = 0;
try
{
// OBJECT CREATION AND PROCESSING
try
{
myObject = new Object();
// Do something with myObject.
}
// EXCEPTION HANDLING
catch (Exception &e)
{
// When there is an excepion, handle or throw,
// else NoException will be thrown.
}
throw NoException();
}
// CLEAN UP
catch (Exception &e)
{
delete myObject;
if (e.isException()) throw e;
}
- No exception thrown by object -> NoException -> Object cleaned up
- Exception thrown by object -> Handled -> NoException -> Object cleaned up
- Exception thrown by object -> Thrown -> Exception -> Object cleaned up -> Thrown