70-外传篇 1
来源:互联网 发布:棉先生衣服怎么样 知乎 编辑:程序博客网 时间:2024/05/18 01:19
1、问题1
2、
function2函数中抛出异常,传递上一层函数function1,function1没有能力捕获处理异常,接着抛给main函数,main函数也无法处理异常,接着向上抛出,异常终止
3、问题2
#include <iostream>using namespace std;class Test {public: Test() { cout << "Test()"; cout << endl; } ~Test() { cout << "~Test()"; cout << endl; }};int main(){ static Test t; throw 1; return 0;}Test()terminate called after throwing an instance of 'int'Aborted (core dumped)terminate 是谁调用的呢
4、
5、
#include <iostream>#include <cstdlib>#include <exception>using namespace std;void my_terminate(){ cout << "void my_terminate()" << endl; //exit(1); //abort();}class Test {public: Test() { cout << "Test()"; cout << endl; } ~Test() { cout << "~Test()"; cout << endl; }};int main(){ set_terminate(my_terminate); static Test t; throw 1; return 0;}在my_terminate()中执行 exit(1)的结果:Test()void my_terminate()~Test()发现,调用exit(1)会将全局对象和静态局部对象的析构函数被调用在my_terminate()中执行 abort()的结果:Test()void my_terminate()Aborted (core dumped)发现,调用abort()函数会异常终止程序,并且异常终止的时候不会调用任何对象的析构函数,不会去释放对象资源
6、问题3
#include <iostream>#include <cstdlib>#include <exception>using namespace std;void my_terminate(){ cout << "void my_terminate()" << endl; //exit(1); abort();}class Test {public: Test() { cout << "Test()"; cout << endl; } ~Test() { cout << "~Test()"; cout << endl; throw 2; }};int main(){ set_terminate(my_terminate); static Test t; throw 1; return 0;}my_terminate()中调用abort()的情况:就会避免对象释放,Test()void my_terminate()Aborted (core dumped)my_terminate()中调用exit(1)的情况:会调用my_terminate()会2次,用对象的析构函数,释放对象,可能会造成对内存的重复释放。Test()void my_terminate()//第一次释放资源~Test()void my_terminate()//第二次释放资源如果在一个堆空间new了一段内存,释放一次,再释放一次会造成不稳定C++中terminate()中默认调用的是abort()而不是exit()abort()强制结束当前的应用程序,不会去调用对应的析构函数,就怕析构函数中抛出异常
7、小结
阅读全文
0 0
- 70-外传篇 1
- 71-外传篇 2
- 72-外传篇 3
- JDBC编程学习笔记整理(外传1)---数据源与JNDI
- iOS外传——偷懒之路(1)
- 《武林外传》
- 武林外传
- 武林外传
- 敏捷外传
- 实习外传
- 视图 --- 外传
- 香农外传
- 杨白劳外传
- [原创][vb6]仙剑3外传的存档修改器--补充说明1设计图示
- 武侠小说:郎咸平外传
- 《武林外传》不错...
- CSDN 韩磊 外传 【ALLyeSNO】
- 武林外传语录
- C++取得系统时间方法
- 慕课网学习项目之答答租车系统
- c语言三阶幻方问题(回溯)
- 原码, 反码, 补码 详解
- java 基础之数组各种操作
- 70-外传篇 1
- bzoj4917 [Lydsy六月月赛]Hash Killer IV
- Servlet+mysql实现分页技术
- 移动架构32_自定义网络访问框架开发
- Gym
- HTTP协议详解①
- 常用STL整理
- ResNet小结
- 用python做数据分析4|pandas库介绍之DataFrame基本操作 by 是蓝先生