这是在标准 C++ 中实现类似最终行为的好方法吗?(无特殊指针)
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;
}
- 对象没有抛出异常 -> NoException -> 对象已清理
- 对象抛出的异常 -> 已处理 -> NoException -> 对象已清理
- 对象抛出的异常 -> 抛出 -> 异常 -> 对象清理 -> 抛出