10.3 异常的重新抛出和catch_all

◆ 1、当catch语句捕获一个异常后,可能不能完全处理异常,完成某些操作后,该异常必须由函数链中更上级的函数来处理,这时catch子句可以 重新抛出 (rethrow)该异常,把异常传递给函数调用链中更上级的另一个catch子句,由它进行进一步处理。

重新抛出表达式仍为:  throw;
但仅有一个关键字,因为异常类型在catch语句中已经有了,不必再指明。

被重新抛出的异常就是原来的异常对象。但是重新抛出异常的catch子句应该把自己做过的工作告诉下一个处理异常的catch子句,往往要对异常对象做一定修改,以表达某些信息,因此catch子句中的异常声明必须被声明为引用,这样修改才能真正做在异常对象自身中。

◆ 2、通用catch子句(catch_all):
     catch(...)
    {/*代码*/}


任何异常都可以进入这个catch子句。这里的三个点称为省略号。花括号中的复合语句用来执行指定操作。

异常发生后按栈展开(stack unwinding)退出,动态分配的非类对象资源不会自动释放的,通常在catch_all子句中释放。例如:
void fun1()
{
       int *res;
       res=new int[100]; //定义一个资源对象
       try{
              //代码包括使用资源res和某些可能引起异常抛出的操作
       } //异常可能有多种
       catch(...)//不论是那种异常都在此释放
       {
              delete [] res; //释放资源对象res
              throw; //重新抛出异常
       }
       delete [] res; //正常退出前释放资源对象res;
}

◆ 3、 catch_all子句 可以单独使用,也可以与其它catch子句联合使用。如果联合使用,它必须放在相关catch子句表的最后。

catch子句被检查的顺序与它们在try块之后排列顺序相同,一旦找到了一个匹配,则后续的catch子句将不再检查,按此规则,catch_all子句(catch(...){})处理表前面所列各种异常之外的异常。

如果只用catch_all子句进行某项操作,则其他的操作应由catch子句重新抛出异常,逆调用链去查找新的处理子句来处理,不能在子句列表中再安排一个处理同一异常的子句,因为第二个子句是永远执行不到的。 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Universal Data Access Components History [email protected] 7.4.12 21-Jan-19 Bug with using the datetime literals in SQL statements in TVirtualQuery is fixed Bug with AV failure in the Refresh method in RAD Studio 10.2 Tokyo on Linux is fixed Oracle data provider Bug with authentication of old users in Oracle 12c is fixed Bug with the DML arrays that contain the TIMESTAMP values is fixed Bug with describe of SYNONYM for stored procedures in the Direct mode is fixed Bug with fetch data when the FieldsAsString option is set to True in the Direct mode is fixed Bug with open REFCURSOR is fixed SQLServer data provider Bug with the "Cannot modify a read-only dataset" error in Android is fixed MySQL data provider Bug with setting the data type of the parameter to BOOLEAN for all TINYINT system data types in the functions is fixed PostgreSQL data provider Bug with reading and writing of the floating point values is fixed InterBase data provider Bug with using the AsBytes property is fixed Bug with the AV failure when using the BLOB data type in Lazarus is fixed SQLite data provider Now the Direct mode is based on the SQLite engine version 3.26.0 DBF data provider Bug with wrong selection of deleted records is fixed Bug with storing numeric values is fixed Bug with using the CTOD, DTOC, DTOS, TTOC, TTOD and EMPTY functions in index expressions is fixed Bug with using a field named RECNO in the index expressions is fixed MongoDB data provider Bug with reading string values in the BSON format is fixed NexusDB data provider Bug with missing files for NexusDB for RAD Studio Rio (10.3) is fixed ODBC data provider The UnknownAsSring specific option is added

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值