C++ 异常处理
终于我们把类与对象的基本知识讲完了!
这篇文章简单讲述C++对于异常的处理
什么是异常
一般来说,程序在运行阶段发生错误,导致程序无法正常运行下去的情况就是异常
比如内存已满,但仍然再请求内存,比如程序打开一个无法识别的文件,比如指针指向了一个未知的地方却仍然调用它做一系列操作等待
调用abort()函数
我们最常用的处理异常的方式就是调用函数abort(),函数的原型在cstdlib中,当程序发生错误时,调用abort()函数提前终止程序,避免引起下面更多不可预知的错误
我们写一个简单的程序验证:
#include <iostream>
#include <cstdio>
using namespace std;
double hmean(double x, double y);
int main()
{
double x, y, z;
cout << "enter two numbers:\n";
int num = 0;
while(num<5)
{
cin >>x>>y;
z = hmean(x, y);
cout << "when we enter the proper number we can see the value : "<<z<<endl;
num++;
}
cout << "all the input are right!\n";
return 0;
}
double hmean(double x, double y)
{
if(x==-y)
{
cout << "untenable arguments to hmean()\n"<<endl;
abort(); // 提前终止程序
}
return x*y/(x-y);
}
程序很简单所以就不给很多注释了,大家可以自行阅读
我们输入两个数,然后返回他们的乘积除以他们的差,当两个数为相反数时,我们抛出异常,否则返回正确的值,当次数达到五次时,说明输入的所有数据都是没有问题的
输出结果:
enter two numbers:
8 9
when we enter the proper number we can see the value : -72//返回值
1 -1
untenable arguments to hmean() //抛出异常
异常机制
发生异常情况时,人为规定的一种响应方式。
一般由三个模块组成
- 引发异常
- 捕获异常
- 使用try模块
这里面对应了三个关键字:try, catch, throw
我们还是通过上面的例子改装一下,看看这三个模块是如何运作的
#include <iostream>
#include <cstdio>
using namespace std;
double hmean(double x, double y);
int main()
{
double x, y, z;
cout << "enter two numbers:\n";
int num = 0;
while(num<3)
{
cin >>x>>y;
try
{
z = hmean(x, y);
}
catch(const char *s)
{
cout << s << endl;
cout << "you should enter a pair of proper numbers: \n";
continue;
}
cout << "when we enter the proper number we can see the value : "<<z<<endl;
num++;
}
cout << "all the input are right!\n";
return 0;
}
double hmean(double x, double y)
{
if(x==-y)
{
throw "we can't divide 0!\n";
}
return x*y/(x-y);
}
先给出结果:
enter two numbers:
2 3
when we enter the proper number we can see the value : -6
1 -1
we can't divide 0!
you should enter a pair of proper numbers:
6 5
when we enter the proper number we can see the value : 30
2 -2
we can't divide 0!
you should enter a pair of proper numbers:
1 2
when we enter the proper number we can see the value : -2
all the input are right!
我们可以从输出结果来看看我们的异常机制是如何工作的
首先我们输入两个数据,然后进入try模块
try
{
z = hmean(x, y);
}
执行try模块中的语句,也就是函数hmean
double hmean(double x, double y)
{
if(x==-y)
{
throw "we can't divide 0!\n";
}
return x*y/(x-y);
}
当我们的数据没有问题,也就是输入的两个数不是相反数时,我们直接返回值,然后因为没有抛出异常(throw)所以我们不需要捕获(catch)异常并处理,我们直接跳过catch模块进行值的输出,所以结果的前三行输出就是这样得来的
enter two numbers:
2 3
when we enter the proper number we can see the value : -6
// 跳过了catch
然后我们输入了一对错误的数据,同样我们进入try模块,然后在函数中我们会抛出异常
if(x==-y)
{
throw "we can't divide 0!\n";
}
当异常抛出时,我们程序就会查找与异常类型相匹配的catch模块,也就是我们throw的是一个字符串,那么我们的catch模块的括号中应该匹配字符串类型
catch(const char *s)//匹配字符串类型的异常
{
cout << s << endl;
cout << "you should enter a pair of proper numbers: \n";
continue;
}
这里我们打印了抛出的错误(throw的内容会传递到const char*s),然后我们指出需要重新输入正确的一对数字,然后这里我们使用了循环中才会使用的continue,跳过while后面的语句回到开头,重新输入数字
所以只有当我们正确的输入三组数据时,程序才会结束;异常时则抛出异常并让你重新输入
注意: 一个try可以有多个catch模块
当然我们可以将异常也封装成类,然后根据异常的不同调用不同的异常类
感谢您的耐心阅读!
本文深入探讨C++中的异常处理机制,包括异常的概念、如何使用abort()函数处理异常,以及异常机制的三个关键部分:引发异常、捕获异常和使用try模块。通过实例演示了如何在代码中有效运用异常处理,确保程序的健壮性和稳定性。
1万+

被折叠的 条评论
为什么被折叠?



