C++异常

基本概念

在C++中所谓的异常就是当一个函数在执行的时候发现一个自己无法处理的错误, 此时就将该异常扔调用自己的函数,或者间接调用自己的一个函数,取处理这个错误, 此时这种情况就叫异常

异常的捕获和抛出

异常通过三个个机制 try 和 catch来捕获以及throw抛出, 编译器在编译的时候通过检查throw后面的类型, 然后选择出一个距离自己最近并且和自己最符合的一个函数取执行异常处理相应的动作. 在异常抛出的时候,函数会将自己的调用堆栈进行展开, 通常先去检查自己内部有没有符合类型的异常处理, 如果有, 就跳到对应的地方, 如果没有, 就查看调用自己的函数有没有符合类型的异常处理机制, 然后将自己的堆栈进行释放, 直到回到main函数时, 此时还没有找到对应的异常处理机制, 此时就退出程序, 在此过程中抛出异常后会释放自己的局部参数, 当异常抛出后, 对应的对象也就还给系统了

异常捕获的匹配原则

被抛出的对象必须和catch后面的对象类型一致, 但是可以将一个非const的对象传给一个const对象, 将派生类的对象传给基类. 同时在异常处理的时候也允许将一个数组传给一个指向数组的指针, 以及将一个函数传给一个指向函数的指针.

class Exception
{
public:
    Exception(int errId, const string& errMsg)
        :_errId(errId)
         ,_errMsg(errMsg)
    {}
    void What() const
    {
        cout << "errId:" << _errId << endl;
        cout << "errMsg" << _errMsg << endl;
    }
private:
    int _errId;
    string _errMsg;
};

void Func1(bool isThrow)
{
    if(isThrow)
    {
        throw Exception(1, "抛出Exception对象");
    }
    cout << "Func1:" <<  isThrow << endl;
}

void Func2(bool IsThrowString, bool IsThrowInt)
{
    if(IsThrowString)
    {
        throw string("抛出异常对象");
    }
    if(IsThrowInt)
    {
        throw 7;
    }
    printf("Func2(%d, %d)\n", IsThrowString, IsThrowInt);
}

void Func()
{
    try
    {
        Func1(false);
        Func2(true, true);
    }
    catch(const string& errMsg)
    {
        cout << "Catch a string object" << errMsg << endl;
    }
    catch(Exception& e)
    {
        e.What();
    }
    catch(int errId)
    {
        cout << "Catch a int object" << errId << endl;
    }
    catch(...)
    {
        cout << "未知异常" << endl;
    }
    printf("Func()\n");
}
异常与析构函数&构造函数

不能在构造函数中抛异常, 因为构造函数是用来初始化的, 此时如果抛出异常就会出现对象内的成员变量没有被完全初始化,或者对象没有被完全初始化
不能在析构函数中抛异常. 析构函数是用来做清理工作的, 当在析构中抛出异常的时候, 此时就会有可能使得某些资源违背释放,此时就会出现问题

异常的重新抛出

有时异常不是直接处理, 而是被重新抛给另外一个执行函数去处理, 此时的这种机制就叫做异常的重新抛出

class Exception
{
public:
    Exception(int errId, const string& errMsg)
        :_errId(errId)
         ,_errMsg(errMsg)
    {}
    void What() const
    {
        cout << "errId:" << _errId << endl;
        cout << "errMsg" << _errMsg << endl;
    }
private:
    int _errId;
    string _errMsg;
};

void Func1(bool isThrow)
{
    if(isThrow)
    {
        throw Exception(1, "抛出Exception对象");
    }
    cout << "Func1:" <<  isThrow << endl;
}

void Func2(bool IsThrowString, bool IsThrowInt)
{
    if(IsThrowString)
    {
        throw string("抛出异常对象");
    }
    if(IsThrowInt)
    {
        throw 7;
    }
    printf("Func2(%d, %d)\n", IsThrowString, IsThrowInt);
}

void Func()
{
    try
    {
        Func1(false);
        Func2(true, true);
    }
    catch(const string& errMsg)
    {
        cout << "Catch a string object" << errMsg << endl;
    }
    catch(Exception& e)
    {
        e.What();
    }
    catch(int errId)
    {
        cout << "Catch a int object" << errId << endl;
    }
    catch(...)
    {
        cout << "未知异常" << endl;
    }
    printf("Func()\n");
}

#include <iostream>
#include <string>
#include <time.h>
#include <vector>
#include <cstdio>
using namespace std;

class Exception
{
public:
    Exception(int errId = 0, const string errMsg = "")
        :_errId(errId)
        ,_errMsg(errMsg)
    {}
    void What()
    {
        cout << "errId:" << _errId << endl;
        cout << "errMsg" << _errMsg << endl;
    }
private:
    int _errId;
    string _errMsg;
};
void Func1()
{
    throw string("Thorw Func1 string");
}

void Func2()
{
    try
    {
        Func1();
    }
    catch(string& errMsg)
    {
        /* cout << errMsg << endl; */
        Exception e(1, "Rethrow Exception");
        /* throw e; */
        throw;
    }
}

void Func3()
{
    try
    {
        Func2();
    }
    catch(Exception& e)
    {
        e.What();
    }
}

上述代码中, 当出现异常的时候, 不是对其直接处理, 而是通过产生一个父类对象, 然后这个对象调用对应的成员变量, 这样在类外面捕获该异常的时候, 只需要对父类的对象进行捕获, 便可以知道是那一层出现错误

异常的优缺点

异常的优点:
异常解决了程序在运行的过程中如果出现自己不能处理的动作时就强制退出的问题.可以告知用户对应的出错原因具体在哪里, 具体是什么
异常的缺点:
异常会使得程序的执行流通过查看代码不能确定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值