c/c++语言开发共享关于C++的异常抛出

在接触 throw 之前,我们只知道可以通过函数的返回值来获取和定位错误,比如通过 return 来层层返回是一种方法,但如果牵扯到多层函数调用,那么通过 return 来返回错误显得过于拖沓,这时就应该用到 throw 这个方法,throw 的灵活强大将在下边详细说说。 首先是 throw 的三个 …

在接触 throw 之前,我们只知道可以通过函数的返回值来获取和定位错误,比如通过 return 来层层返回是一种方法,但如果牵扯到多层函数调用,那么通过 return 来返回错误显得过于拖沓,这时就应该用到 throw 这个方法,throw 的灵活强大将在下边详细说说。

 

首先是 throw 的三个关键字:

throw:这是个抛出的关键字,就像是return一样,他可以给上一层的调用者返回一个异常,抛出的异常可以是常见的类型,如int,char,指针,结构体甚至是类。

try:来捕获异常,try的作用域(这个作用域叫做保护段)中将会包含拥有throw的函数,如果没有抛出异常,代码将会一直执行下去并跳出 try

catch:catch用来输出异常,他通常跟在 try 之后,如果在try后跟着多个catch,也只会输出一个catch,抛出来的异常将会找到合适的类型进行输出,输出后将不会再进入其他catch输出,如果找不到合适的类型的 catch 编译将会报错。

 

下面是一个基本的代码例子,说明 throw、trycatch的基本用法,与 catch 的类型自动匹配:

 1 #include <iostream>  2 #include <string>  3   4 using namespace std;  5   6 int test_1(int num)  7 {  8     if (num != 0)  9     { 10         throw - 1;                                //抛出一个int类型的异常,如果 30 行传1过来,那么将会抛出该异常至36行的 try 11     } 12     else { 13         throw new string("抛出字符串异常");         //抛出一个字符串异常,如果 30 行传0过来,那么将会抛出该字符串异常,catch将会匹配42行 14     } 15  16     return 0; 17 } 18  19 int test_2(int num)                                //函数进行了嵌套 20 { 21     test_1(num); 22     return 0; 23 } 24  25 int main() 26 { 27     int ret = 0; 28     try                                                     //try中的方法的返回值会被下边的catch捕捉 29     { 30         ret = test_2(1);                                    //传1过去,将会捕获test_1中的throw -1,将会直接跳出至41行。 31     } 32     catch (int error) {                                    //捕捉到的值会传到 error 变量 33         printf("出现异常了!%dn", error); 34     } 35     catch (string * error) 36     { 37         printf("捕捉到字符串异常:%s", error->c_str());        //如果30行传过去的是0,可以通过抛出的异常来找合适的类型 string 38         delete error; 39     } 40     catch (...) {                                            //如果没有合适的类型将会进入这里的通配,如果没有这行通配,你试着传个浮点型过来,编译不会过的。 41         printf("catch...n"); 42     } 43  44     return 0; 45 }

 

 

这里在说一下抛出被拦截的情况,同样是上边的代码:

 1 #include <iostream>  2 #include <string>  3   4 using namespace std;  5   6 int test_1(int num)  7 {  8     if (num != 0)  9     { 10         throw - 1;                                  //抛出一个int类型的异常,返回到 20 行 11     }else{ 12         throw new string("抛出字符串异常"); 13     } 14  15     return 0; 16 } 17  18 int test_2(int num) 19 { 20     try                                            //如果这里捕获异常,第38行中的 try 将捕获不到test_1的异常 21     { 22         test_1(num); 23     } 24     catch (...) { 25         printf("test_2 异常抛出"); 26         throw 0.01; 27     } 28  29     /* throw */ 30     //这里如果再 throw ,38行将会再次接收到 test_1 的异常 31  32     return 0; 33 } 34  35 int main() 36 { 37     int ret = 0; 38     try 39     { 40         ret = test_2(1);                                    //传1过去,将会抛出 test_1 中的 throw -1 41     } 42     catch (int error) { 43         printf("出现异常了!%dn", error);         44     } 45     catch (string * error)  46     {                         47         printf("捕捉到字符串异常:%s", error->c_str()); 48         delete error; 49     } 50     catch (...) { 51         printf("catch...n"); 52     } 53  54     return 0; 55 }

 

待续……

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/600234.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐