effective c++为异常安全而努力是值得的(1)

如果我们假设函数带着“空白的异常明细”(empty exception specification)者必为nothrow函数

int doSomething() throw();//空白的异常明细

这并不是说doSomething绝对不会抛出异常,而是说如果doSomething抛出异常,将会是严重的错误,会有你意想不到的函数被调用
然后上cplusplus reference看看了关于set_unexcepted 和 bad_exception的知识

// set_unexpected example
#include <iostream>       // std::cerr
#include <exception>      // std::set_unexpected

void myunexpected () {
  std::cerr << "unexpected called\n";
  throw 0;     // throws int (in exception-specification)
}

void myfunction () throw (int) {
  throw 'x';   // throws char (not in exception-specification)
}

int main (void) {
  std::set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { std::cerr << "caught int\n"; }
  catch (...) { std::cerr << "caught some other exception type\n"; }
  return 0;
}

unexpected_handler set_unexpected (unexpected_handler f) throw();
Sets f as the unexpected handler function.
设置f为unexpected handler function.
The unexpected handler function is a function automatically called when a function throws an exception that is not in its dynamic-exception-specification (i.e., in its throw specifier).
当一个函数抛出异常,并且异常不在dynamic-exception-specification中时,unexpected handler function将会自动调用

The unexpected handler function can handle the exception and shall end either by teminating (calling terminate or some other method, such as exit or abort) or by throwing an exception (even rethrowing the same exception again). If the exception thrown (or rethrown) is not in the function’s dynamic-exception-specification but bad_exception is, a bad_exception is thrown. Otherwise, if the new exception is not in the dynamic-exception-specification either, terminate is automatically called.
unexpected handler function能够处理异常,强制停止程序(通过exit或者abort)或者重新抛出异常,如果抛出的异常不在function’s dynamic-exception-specification中,但是bad_exception在function’s dynamic-exception-specification中,那么bad_exception被抛出。否则,如果新抛出的异常依旧不在 dynamic-exception-specification中,程序将会被自动终止

Before this function is called by the program for the first time, the default behavior is to call terminate.
如果没有这个函数的话(就是没有set_unexpected)那么默认行为是程序终止

// set_unexpected example
#include <iostream>       // std::cerr
#include <exception>      // std::set_unexpected

void myunexpected () {
  std::cerr << "unexpected called\n";
  throw 0;     // throws int (in exception-specification)
}

void myfunction () throw (int) {
  throw 'x';   // throws char (not in exception-specification)
}

int main (void) {
  std::set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { std::cerr << "caught int\n"; }
  catch (...) { std::cerr << "caught some other exception type\n"; }
  return 0;
}
output:
unexpected called
caught int

Process returned 0 (0x0)   execution time : 0.017 s
Press any key to continue.
// bad_exception example
#include <iostream>       // std::cerr
#include <exception>      // std::bad_exception, std::set_unexpected

void myunexpected () {
  std::cerr << "unexpected handler called\n";
  throw 'x';//抛出的异常依旧不在exception-specification中,所以抛出bad_exception
}

void myfunction () throw (int,std::bad_exception) {
  throw 'x'; // throws char (not in exception-specification)
}

int main (void) {
  std::set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { std::cerr << "caught int\n"; }
  catch (std::bad_exception be) { std::cerr << "caught bad_exception\n"; }
  catch (...) { std::cerr << "caught some other exception\n"; }
  return 0;
}
unexpected handler called
caught bad_exception

Process returned 0 (0x0)   execution time : 0.014 s
Press any key to continue.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值