C++ Exception

Exception type:

Derived types (scattered throughout different library headers)


Indirectly (through  logic_error ):

Indirectly (through  runtime_error ):

Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed.

If an ellipsis (...) is used as the parameter of catch, that handler will catch any exception no matter what the type of the exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers:

1
2
3
4
5
6
try {
  // code here
}
catch (int param) { cout << "int exception"; }
catch (char param) { cout << "char exception"; }
catch (...) { cout << "default exception"; }
 


In this case, the last handler would catch any exception thrown of a type that is neither  int  nor  char .

After an exception has been handled the program, execution resumes after the  try-catch  block, not after the  throw statement!.

It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example: 

1
2
3
4
5
6
7
8
9
10
11
try {
  try {
      // code here
  }
  catch (int n) {
      throw;
  }
}
catch (...) {
  cout << "Exception occurred";
}

Exception specification

Older code may contain  dynamic exception specifications. They are now deprecated in C++, but still supported. A  dynamic exception specification follows the declaration of a function, appending a  throw specifier to it. For example:

 
double myfunction (char param) throw (int);
 


This declares a function called  myfunction, which takes one argument of type  char and returns a value of type  double. If this function throws an exception of some type other than  int, the function calls  std::unexpected instead of looking for a handler or calling  std::terminate.

If this  throw specifier is left empty with no type, this means that  std::unexpected is called for any exception. Functions with no  throw specifier (regular functions) never call  std::unexpected, but follow the normal path of looking for their exception handler.

1
2
int myfunction (int param) throw(); // all exceptions call unexpected
int myfunction (int param);         // normal exception handling 

void unexpected();
Function handling unexpected exceptions
Calls the current  unexpected handler.

By default, the  unexpected handler calls  terminate. But this behavior can be redefined by calling  set_unexpected.

This function is automatically called when a function throws an exception that is not listed in its  dynamic-exception-specifier (i.e., in its  throw specifier).

This function is provided so that the  unexpected handler can be explicitly called by a program, and works even if set_unexpected has not been used to set a custom  unexpected handler (calling  terminate in this case).



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值