当前位置: 首页 > news >正文

【C++学习记录】为什么需要异常处理,以及Try Catch的使用方法

1.什么是异常,什么是错误?

程序无法保证100%正确运行,万无一失。有的错误在编译时能发现,比如:关键字拼写、变量名未定义、括号不配对、语句末尾缺分号等。这是在编译阶段发现的,称为编译错误。
有的能正常通过编译,也能运行,但在运行过程中出现异常。出现异常,系统不应该崩溃退出,而应该给用户提示,让他改对输入。
比如:计算过程,出现除数为0, 内存空间不够、无法打开输入文件。 这体现了系统的容错能力。
**异常处理:**对运行时出现的差错以及其它例外情况的处理。

2.如何处理异常

#include<iostream>
#include<cmath>
using namespace std;
int main(){double triangle(double,double,double);cout << "pls input three double num and make a triangle:" << endl;double a,b,c, area;cin >> a >> b >> c;try{while(a>0&&b>0&&c>0){area = triangle(a,b,c);}}catch(double){cout << "error!" << endl;}catch(const std::exception& e){std::cerr << e.what() << '\n';}cout << "the result:" << area << endl;return 0;}double triangle(double a,double b,double c){double area;double s = (a+b+c)/2;if(a+b<=c||a+c<=b||b+c<=a){throw a;}area = sqrt(s*(s-a)*(s-b)*(s-c));return area;
}

可以捕获任何类型的错误,用来兜底使用

catch(....){cout << "ERROR! << endl;
}

上层调用侧将异常信息原样抛出

catch(int){throw;// 将已捕获的异常信息再次原样输出
}

3.在函数声明中进行异常情况指定

php7 开始有了这种情况,原来是从C++来的

double triangle(double,double,double) throw(double);

表示函数只能抛出double类的异常。

double triangle(double,double,double) throw(double,int, float, char);

表示函数只能抛出double,int, float, char类的异常。

表示一个不能抛出异常的函数,程序将非正常终止

double triangle(double,double,double) throw();

4. 进阶

4.1 在异常处理中处理析构函数?

我记得php中也对此有过讨论。好像还能手动修改。
先执行 destruct 在执行 catch

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Student{
public:Student(int n, string nam){num = n;name = nam;cout << "constructor-" << num << endl;}~Student(){cout << "destructor-" << num << endl;}void get_data();
private:int num;string name;};void Student::get_data(){if (num==0){throw num;}else{cout << num << " " << name << endl;cout << "in get_data() " << endl;}
}void fun(){Student stud1(1101,"Tan");stud1.get_data();Student stud2(0,"li");stud2.get_data();
}int main(){cout << "main begin" << endl;cout << "call fun()" << endl;try{fun();}catch(int n){cout << "num="<<n<<",error!"<<endl;}cout << "main end" << endl;return 0;}

输出内容:

main begin
call fun()
constructor-1101
1101 Tan
in get_data()
constructor-0destructor-0destructor-1101
num=0,error!
main end

4.2 如何在catch中获取堆栈信息?

http://www.lryc.cn/news/142567.html

相关文章:

  • 孪生网络(Siamese Network)
  • 【Redis】Redis是什么、能干什么、主要功能和工作原理的详细讲解
  • 8月26日,每日信息差
  • 云和恩墨面试(部分)
  • volatile 关键字详解
  • Ceph入门到精通-大流量10GB/s LVS+OSPF 高性能架构
  • Unity光照相关
  • Qt基本类型
  • 前端基础(Element、vxe-table组件库的使用)
  • C++学习记录——이십팔 C++11(4)
  • UE学习记录03----UE5.2 使用拖拽生成模型
  • Spring Cache框架(缓存)
  • Linux学习之Ubuntu 20使用systemd管理OpenResty服务
  • [数据集][目标检测]疲劳驾驶数据集VOC格式4类别-4362张
  • matlab使用教程(25)—常微分方程(ODE)选项
  • MybatisPlus简单到入门
  • 9. 优化器
  • go学习之流程控制语句
  • docker基于已有容器和通过Dockerfile进行制作镜像配置介绍
  • 2022年09月 C/C++(四级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 二级MySQL(九)——表格数据处理练习
  • QT ListQvector at赋值出错以及解决办法 QT基础入门【QT存储结构】
  • STM32 CubeMX (H750)RGB屏幕 LTDC
  • Redis问题集合(三)在Redis容器里设置键值对
  • spark中排查Premature EOF: no length prefix available
  • numpy高级函数之where和extract函数
  • 用Python写一个武侠游戏
  • Java --- 异常处理
  • CDN/DCDN(全站加速)排查过程中如何获取Eagle ID/UUID
  • 网络安全应急响应预案培训与演练目的