【C++】异常 Exception

异常

  • 编程过程中的常见错误类型
    • 语法错误
    • 逻辑错误
    • 异常

异常是一种在程序运行过程中可能会发生的错误(比如内存不够)

异常如果没有被处理,会导致程序终止。

如果觉得这个操作可能会抛出异常(系统抛出的),就使用try-catch,一旦try里发生异常就转到catch部分

for (int i = 0; i < 99999; i ++) {
	try {
        int *p = new int[99999999]; // 可能会内存不够抛出异常
    } catch(...) { // 写...表示无论try里什么异常都会捕捉
        cout << "Exception. Memory not enough." << endl;
        break;
    }
}
  • 也可以主动抛出异常

    因为有些异常不会被主动抛出,不会导致程序终止。

    int main() {
        try {
            int a = 10;
            int b = 0;
            int c = a / b;
            cout << c << endl;
        } catch (...) {
            cout << "Exception." << endl;
        }
        
        getchar();
        return 0;
    }
    

    以上代码执行后不会输出Exception也不会输出c的值,直接闪退。尽管除0是异常操作,但程序并不会抛出异常,这样很危险,因为捕捉不到异常,无法处理异常操作。

    那么系统不抛就自己抛

    int divide(int v1, int v2) {
        if (v2 == 0) {
            // throw exception
            throw 999;
        }
        return v1 / v2;
    }
    
    int main() {
        try {
            int a = 10;
            int b = 0;
            cout << divide(a, b) << endl;
        } catch (int exception) {
            cout << "Exception:" << exception << endl;
        }
        
        getchar();
        return 0;
    }
    

    当除数为0的时候抛出异常,可以被catch,然后输出Exception:999.

    如果抛出的异常为字符串,catch异常的类型也要发生变化。

    int divide(int v1, int v2) {
        if (v2 == 0) {
            // throw exception
            throw "不能除以0";
        }
        return v1 / v2;
    }
    
    int main() {
        try {
            int a = 10;
            int b = 0;
            cout << divide(a, b) << endl;
        } catch (const char* exception) {
            cout << "Exception:" << exception << endl;
        }
        
        getchar();
        return 0;
    }
    

    输出:Exception:不能除以0

    • try-catch格式:

      在这里插入图片描述

      可以捕捉不同类型的异常。

    throw异常后,会在当前函数中查找匹配的catch,找不到就终止当前函数代码,去上一层函数中查找。如果最终都找不到匹配的catch,整个程序就会终止。

  • 异常的抛出声明

    为了增强可读性和方便团队协作,如果函数内部可能会抛出异常,建议函数声明一下异常类型.
    在这里插入图片描述 表示可能会抛出int类型的异常。在这里插入图片描述

  • 自定义异常类型

    // 所有异常的基类
    class Exception {
    private:
    public:
        virtual const char *what() const = 0;
        virtual int code() const = 0;
    };
    
    class DivideException: public Exception {
        const char *what() const {
            return "不能除以0";
        }
        int code() const {
            return 202; // 举例
        }
    };
    
    class AddException: public Exception {
        const char *what() const {
            return "加法有问题";
        }
        int code() const {
            return 303;
        }
    }
    
    int divide(int v1, int v2) {
        if (v2 == 0) {
            // throw exception
            throw DivideException();
        }
        return v1 / v2;
    }
    
    
    int main() {
        try {
            int a = 10;
            int b = 0;
            cout << divide(a, b) << endl;
        } catch (const Exception &exception) {
            cout << "DivideException:" << Exception.what() << endl;
        }
        
        getchar();
        return 0;
    }
    
  • 标准异常(std)

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值