c++-------异常

目录

一、异常基本语法

二、栈解旋

三、异常变量的生命周期

四、异常遇上多态

五、系统标准异常


一、异常基本语法

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

class MyException {
public:
    void MyDivision()
    {
        cout << "我的异常" << endl;
    }
};

int Exception(int a, int b)
{
    if (b == 0)
    {
        //throw - 1;    //抛出int类型异常
        //throw 'A';    //抛出char类型异常
        //throw 3.14;     //抛出double类型异常
        //string str = "aaa";      //抛出string类型异常
        //throw str;
        throw MyException();
    }
    return a / b;
}

void test01()
{
    int a = 10;
    int b = 0;
    try
    {
        Exception(a, b);
    }
    catch (int)
    {
        cout << "int类型异常捕获" << endl;
    }
    catch (char)
    {
        cout << "char类型异常捕获" << endl;
    }
    catch (double)
    {
        //捕获到异常但是不处理    继续向上抛出这个异常
        //异常必须有函数进行处理   否则系统会调用terminate函数  让程序中断
        throw;
        cout << "double类型异常捕获" << endl;
    }
    catch (MyException e)
    {
        e.MyDivision();
    }
    catch (...)
    {
        cout << "其他类型异常捕获" << endl;
    }
}

int main()
{
    try 
    {
        test01();
    }
    catch (double)
    {
        cout << "main  double类型异常捕获" << endl;
    }

    system("pause");
    return EXIT_SUCCESS;

}

二、栈解旋

从try代码开始到throw抛出异常之前,所有栈上的数据都会被释放,释放的顺序和创建的顺序相反,这个过程我们称为栈解旋

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

class MyException {
public:
    void MyDivision()
    {
        cout << "我的异常" << endl;
    }
};

class Person {
public:
    Person()
    {
        cout << "Person默认构造" << endl;
    }
    ~Person()
    {
        cout << "Person析构函数" << endl;
    }
};

int MyDivision(int a ,int b)
{
    if (b == 0)
    {
        //从try代码开始到throw抛出异常之前  所有栈上的数据都会被释放    
        //释放的顺序和创建的顺序相反 这个过程我们称为栈解旋
        Person p1;
        Person p2;
        throw MyException();
    }
    return a/b;

}

void test01()
{
    int a = 10;
    int b = 0;
    try
    {
        MyDivision(a, b);
    }
    catch (MyException e)
    {
        e.MyDivision(); 
    }
}

int main()
{
    test01();

    system("pause");
    return EXIT_SUCCESS;

}

三、异常变量的生命周期

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

class MyException {
public:
    MyException()
    {
        cout << "默认构造函数调用" << endl;
    }
    MyException(const MyException& e)
    {
        cout << "拷贝构造函数调用" << endl;
    }
    ~MyException()
    {
        cout << "析构函数调用" << endl;
    }
};

void DoWork()
{
    throw MyException();
    //throw new MyException();
}



void test01()
{
    try
    {
        DoWork();
    }
    //抛出的是throw MyException()   catch (MyException e)   调用拷贝构造函数    效率低
    //抛出的是throw MyException()   catch (MyException &e)   只调用默认构造函数    效率高
    catch (MyException &e)
    {
        cout << "异常捕获" << endl;
        //delete e;
    }
}

int main()
{
    test01();
    system("pause");
    return EXIT_SUCCESS;

}

四、异常遇上多态

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

class BaseException {
public:
    virtual void PrintError() = 0;
};
//空指针异常
class NULLPointException : public BaseException {
public:
    void PrintError()
    {
        cout << "空指针异常" << endl;
    }
};
//越界异常
class OutOfRange : public BaseException {
public:
    void PrintError()
    {
        cout << "越界异常" << endl;
    }
};

void doWork()
{
    //throw NULLPointException();
    throw OutOfRange();
}

void test01()
{
    try
    {
        doWork();
    }
    catch (BaseException& e)
    {
        e.PrintError();
    }
}

int main()
{
    test01();

    system("pause");
    return EXIT_SUCCESS;

}

五、系统标准异常

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>  
using namespace std;

class Person {
public:
    Person(int age)
    {
        if (age < 0 || age > 150)
        {
            throw out_of_range("年龄必须大于0且小于150");
            //throw length_error("年龄必须大于0且小于150");
        }
        else
        {
            this->m_Age = age;
        }
    }
    

    int m_Age;
};

void test01()
{
    try
    {
        Person(151);
    }
    catch (out_of_range& e)
    //catch (exception& e)
    {
        cout << e.what() << endl;
    }
}

int main()
{
    test01();
    system("pause");
    return EXIT_SUCCESS;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值