从小白开始学C++ 异常(abort)

本文深入探讨C++中的异常处理机制,包括异常的概念、如何使用abort()函数处理异常,以及异常机制的三个关键部分:引发异常、捕获异常和使用try模块。通过实例演示了如何在代码中有效运用异常处理,确保程序的健壮性和稳定性。


终于我们把类与对象的基本知识讲完了!

这篇文章简单讲述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模块

当然我们可以将异常也封装成类,然后根据异常的不同调用不同的异常类

感谢您的耐心阅读!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国家一级假勤奋研究牲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值