#include<iostream>
using namespace std;
/*知识点*/
//1 发生异常之后 是跨函数的 例如在下面测试案例中,用ceshi()函数作为过渡,当有异常的情况下,设置断点发现直接从chu()函数的try调到main的catch中,不再进行ceshi()函数的一个过渡
//2 接受异常之后可以不处理,继续抛出 直接写throw 不做任何处理
//3 catch异常的时候,按照类型进行
//4 异常严格按照类型进行匹配 throw一个int型的变量,则catch一定要是int型,不可以catch 例如char型
int chu(int x, int y)
{
if (y == 0)
{
throw x; //可以理解为return 直接会跳转到相应的catch上
}
return x / y;
}
int ceshi(int x,int y)
{
return chu(x, y);
}
void main()
{
try
{
cout << ceshi(100, 3) << endl;
cout << ceshi(100, 0) << endl;
}
catch (int e)
{
cout << e << "被0除了" << endl;
}
catch (...) //代表处理其余未知的错误
{
cout << "出现未知错误" << endl;
}
system("pause");
}
C++ 异常的处理 基本语法 与注意准则
最新推荐文章于 2024-03-10 00:47:12 发布