Item 39. 异常安全之函数(Exception Safe Functions)

博客指出编写异常安全代码的难点在于抛出和捕获异常之间的操作,函数弹栈前需清理资源。以 String 赋值操作和 Button 的 setAction 方法为例,说明错误写法会使对象处于不良状态,应先处理可能引发异常的操作,再用无异常操作结束过程。

Item 39. Exception Safe Functions

编写异常安全代码的难点不在于抛出和捕获异常,而是在于抛出和捕获之间要做的事情。当异常从抛出

到达捕获语句的过程中,这期间执行的函数在弹栈前需要清理它所使用的任何资源。通常,这就需要片

刻的思考以及一些常识。

以String的赋值操作为例:
 
String &String::operator =( const char *str ) {
    if( !str ) str = "";
    char *tmp = strcpy( new char[ strlen(str)+1 ], str );
    delete [] s_;
    s_ = tmp;
    return *this;
}

char *tmp 这个中间变量似乎有点多余,我们“可以”写成这样:
String &String::operator =( const char *str ) {
    delete [] s_;                                       
    if( !str ) str = "";
    s_ = strcpy( new char[ strlen(str)+1 ], str );
    return *this;
}

果真如此吗?
delete [] 根据约定可以保证不抛出异常,然而new[]可能抛出异常。在未知新的内存是否分配成功的

时候,我们却把s_的内存释放掉了。于是,String对象处于一个bad state。
Herb Sutter 告诉我们在这种情况下应该这样处理:首先在不改变重要状态的一边处理那些能够引发异

常的操作,而后用不能引发异常的操作结束整个过程(First do anything that could cause an

exception "off to the side" without changing important state, and then use operations that

can't throw an exception to finish up.)。

再看一例:

错误的写法:
void Button::setAction( const Action *newAction ) {             
    delete action_; // change state!                            
    action_ = newAction->clone(); // then maybe throw?          

繁琐的写法:
void Button::setAction( const Action *newAction ) {             
    delete action_;                                             
    try {                                                       
        action_ = newAction->clone();                           
    }                                                           
    catch( ... ) {                                              
        action_ = 0;                                            
        throw;                                                  
    }                                                           
}  

简单正确的写法:
void Button::setAction( const Action *newAction ) {
    Action *temp = newAction->clone(); // off to the side...
    delete action_; // then change state!
    action_ = temp;
}

转载于:https://www.cnblogs.com/aiwz/archive/2005/04/22/6333205.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值