读书笔记--异常处理(4)

异常说明:
  不能确定函数是否跑出异常及抛出哪种异常,则用异常说明。
void recoup(int) throw(runtine_error);
  空列表指出寒暑不跑出任何异常
void no_problem() throw();
  如果函数跑出了没有异常说明中列出的异常,调用库函数unexpected.默认情况下,unexpected函数调用terminate函数,终止程序
 
异常说明于虚函数:
  基类与派生类中对虚函数异常说明不同,但派生类虚函数的异常说明与对应基类虚函数的异常说明更严格
当使用指向积累类型的指针调用派生类虚函数时,派生类的异常说明不能增加新的可抛出异常。
class Base
{
 public:
  vitual double f1(double) throw();
  vitual int f2(int) throw(logic_error);
  vitual string f3(string) throw(logic_error,runtime_error);
};
calss Derived:public Base
{
 public:
  //error:exception specification is less restrictive tahn Base::f1
  double f1(double) throw(underflow_error);
  //ok:same exception specification as Base::f2
  int f2(int) throw(logic_error);
  //ok:Derived f3 is more restrictive
  string f3() throw();
};

派生类不能在异常说明列表中增加异常,继承层次的用户应该能够编写依赖于说明函数列表的代码,如果通过基类指针或引用进行函数调用,那么类的用户所涉及的应该只是在基类中指定的异常。基类中的异常列表示虚函数的派生类版本可以抛出的异常列表的超集。
void compute(Base *pb) throw()
{
 try()
 {
  pb->f3();
 }
 catch(const logic_error &le){//...}
 catch(const runtime_error &rl){//...}
}


函数指针的异常说明:
void (*pf)(int) throw(runtime_error);
  在另外一个指针初始化带异常说明的函数的指针,或者将后者复制给函数地址的时候,两个指针的异常说明不必相同,但是,源指针的异常说明必须至少与目标指针一样
//ok:recoup is as restrictive as pf1
void recoup(int) throw(runtime_error);

//ok:recoup is more restrictive than pf2
void (*pf1)(int) throw(runtime_error)=recoup;

//ok: recoup is less restrictive than pf3
void (*pf2)(int) throw(runtime_error,logic_error)=recoup;


 
 
 
 
 
 
 
 
 
 
 
 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值