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

来源:互联网 发布:诛仙三卡级软件 编辑:程序博客网 时间:2024/05/21 07:46

如果我们假设函数带着“空白的异常明细”(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_unexpectedvoid 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_unexpectedvoid 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 calledcaught intProcess returned 0 (0x0)   execution time : 0.017 sPress any key to continue.
// bad_exception example#include <iostream>       // std::cerr#include <exception>      // std::bad_exception, std::set_unexpectedvoid 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 calledcaught bad_exceptionProcess returned 0 (0x0)   execution time : 0.014 sPress any key to continue.
1 0