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.
- C++之为“异常安全”而努力是值得的(29)---《Effective C++》
- effective c++为异常安全而努力是值得的(1)
- 《Effective C++》读书笔记之item29:为“异常安全”而努力是值得的
- 《Effective C++》学习笔记条款29 为“异常安全”而努力是值得的
- Effective C++:条款29:为“异常安全”而努力是值得的
- 读书笔记《Effective C++》条款29:为“异常安全”而努力是值得的
- Effective C++ Item 29 为”异常安全”而努力是值得的
- Effective 阅读笔记---条款29"为异常安全而努力是值得的"
- 条款29:为"异常安全"而努力是值得的
- 条款29:为"异常安全"而努力是值得的
- 为“异常安全”而努力是值得的
- item29: 为“异常安全”而努力是值得的
- 条款29:为“异常安全”而努力是值得的
- 条款29:为“异常安全”而努力是值得的
- 条款29 为“异常安全”而努力是值得的
- 《Effect C++》学习------条款29:为“异常安全”而努力是值得的
- 为“异常安全的努力”是值得的——effective c++学习笔记
- 条款29-30:为“异常安全”而努力是值得的与inlining的里里外外
- 手动安装liberty版本openstack环境(allinone)---安装glance
- bzoj-3144 切糕
- HDU 2041 超级楼梯
- hdoj 5578 Friendship of Frog 【水题】
- C++类的保护成员学习笔记 http://blog.csdn.net/qhs1573/article/details/14109463
- effective c++为异常安全而努力是值得的(1)
- 我的第一篇博客
- MySQL 忘记密码:skip-grant-tables
- jQuery.getJSON() 函数详解
- SPFA算法 Bellman_ford优化
- hdoj 5583 Kingdom of Black and White 【模拟】
- 有向图强连通分量的Tarjan算法
- 配置较为稳定的kali源
- Android验证码简单设计