C++_异常问题

来源:互联网 发布:淘宝上那些代购是真的 编辑:程序博客网 时间:2024/06/07 02:37

一.异常的基本语法



1.异常的最基本的语法

void fun(int x, int y){if (y == 0)//抛出异常的语句{throw y;//抛出y}cout << "x/y=" << x / y << endl;}int main(){try{fun(100, 0);//检测这个函数中的异常}catch(int e)//处理异常的语句{cout << "出现异常 y=" << e << endl;}system("pause");return 0;}


2.如果没有异常,则try后面的catch语句就不执行·

void fun(int x, int y){if (y == 0)//抛出异常的语句{throw y;//抛出y}cout << "x/y=" << x / y << endl;}int main(){try{fun(100, 1);//检测这个函数中的异常}catch(int e)//处理异常的语句{cout << "出现异常 y=" << e << endl;}system("pause");return 0;}

3.异常是跨函数的·


4.接受异常不处理的话可以在抛出,最后如果还是没有被处理的话,系统会终止~

void fun(int x, int y){if (y == 0)//抛出异常的语句{throw y;//抛出y}cout << "x/y=" << x / y << endl;}void test(){try{fun(100,0);//检测这个函数中的异常}catch (...)//处理异常的语句{cout << "我接收了fun()中的异常但是没有处理,继续向上抛出" << endl;throw;//继续向上抛出}}int main(){try{test();}catch (...){cout << "我接受了从下面抛出的异常,但是我也不处理,等着程序终止吧~" << endl;throw;//继续向上抛出,由于main函数外无接异常的函数,程序终止}system("pause");return 0;}

5.捕捉异常是按照严格的类型匹配的,不会进行隐式转换~

void fun(int x, int y){if (y == 0)//抛出异常的语句{throw y;//抛出y}cout << "x/y=" << x / y << endl;}int main(){try{fun(100,0);}catch (char e){;}system("pause");return 0;}

程序终止~

二.异常处理的基本思想




三.栈解旋

概念:


class stu{private:int a;int b;public:stu(int a, int b){this->a = a;this->b = b;cout << "执行构造函数" << endl;}~stu(){cout << "执行析构函数"<<this->a << endl;}};void fun(){stu c1(1, 2), c2(2, 3), c3(3, 4);cout << "接下来发生异常" << endl;throw 1;}int main(){try{fun();}catch (int e){;}system("pause");return 0;}

四.异常接口声明


简单讲如下:


五.异常类型和异常变量的声明周期

看如下代码:

1.catch接受类型为元素变量

class A{public:A(){cout << "执行构造函数" << endl;}A(const A &n){cout << "执行拷贝构造函数" << endl;}~A(){cout << "执行析构函数" << endl;}};void fun(int x, int y, int z){if (x == 0){throw A();//抛出去的时候创建匿名对象,copy给catch中的e}}int main(){try{fun(0,1,1);}catch (A e){cout << "A 类型的异常" << endl;}system("pause");return 0;}
经检验如下:

2.catch类型为引用

class A{public:A(){cout << "执行构造函数" << endl;}A(const A &n){cout << "执行拷贝构造函数" << endl;}~A(){cout << "执行析构函数" << endl;}};void fun(int x, int y, int z){if (x == 0){throw A();//抛出去的时候创建匿名对象,copy给catch中的e}}int main(){try{fun(0,1,1);}catch (A &e){cout << "A 类型的异常" << endl;}system("pause");return 0;}

3.catch类型为指针类型,(注意:指针可以和元素和引用同时存在,但是引用和元素不能同时存在,下面代码没有给出,自己去测试)
class A{public:A(){cout << "执行构造函数" << endl;}A(const A &n){cout << "执行拷贝构造函数" << endl;}~A(){cout << "执行析构函数" << endl;}};void fun(int x, int y, int z){if (x == 0){throw A();//抛出去的时候创建匿名对象,copy给catch中的e}}int main(){try{fun(0,1,1);}catch (A *e){cout << "A 类型的异常" << endl;}system("pause");return 0;}
这样会是什么结果呢?

很明显没有接到异常,程序出错,为什么呢?很明显因为throw一个对象,而接收的是指针,所以类型不匹配。(因为异常是严格的类型匹配)

怎么改就对了呢?

void fun(int x, int y, int z){if (x == 0){throw &(A(10));//抛出去的时候创建匿名对象,copy给catch中的e}}
给抛出加&?这样的话throw创建对象后又被析构,catch的e指向的是废空间

可以看到构造完直接析构了

那应该怎么做呢?

可以new 一个对象,如下代码

class A{public:A(){cout << "执行构造函数" << endl;}A(const A &n){cout << "执行拷贝构造函数" << endl;}~A(){cout << "执行析构函数" << endl;}};void fun(int x, int y, int z){if (x == 0){throw new A;//抛出去的时候创建匿名对象,copy给catch中的e}}int main(){try{fun(0,1,1);}catch (A *e){cout<<"A类型的异常" << endl;delete e;}system("pause");return 0;}
但是记得delete,不然就内存泄露了
六.关于标准异常库的相关用法

下去自己查吧,懒得写了或者等后面闲了在补上,因为现在我快疯了!!!哈哈!!





原创粉丝点击