码迷,mamicode.com
首页 > 编程语言 > 详细

c++之异常

时间:2020-01-14 00:00:48      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:设置   other   cal   exce   closed   float   his   src   lap   

一句话概括:捕获异常:

谁捕获异常?
捕捉谁?---------------------A捕捉B--------------------------A()
                        {
                          try{
                            B();
                           }

                          C();
                        }

谁制造了异常?-----------函数B-----------------------------B()

                        {
                          throw 某个对象:
                        }

捕获后怎么处理?-------------随A怎么做-------------------A()

                        {
                          try{
                            B();
                            } catch(类形 对象)
                          {
                            //处理
                          }
                          }

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 
 5 using namespace std;
 6 
 7 void C ()
 8 {
 9     throw 1;
10 }
11 
12 void B ()
13 {
14     C();
15 }
16 void A()
17 {
18     try {
19         B();
20     }catch (int i)
21     {
22         cout<<"catch exception"<<i<<endl;
23     }
24 }
25 int main(int argc,char**argv)
26 {
27     A();
28     return 0;
29 }
View Code

运行结果:

技术图片

函数B可能抛出多种异常---------------------{1)函数A中可有多个catch分支

                   2)catch分支中,对于异常对象,先捕获派生类对象,再捕获基类对象,按此顺序排放代码

                   3)未能捕获的异常,将继续抛给上一层的调用者}

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 void C (int i)
 9 {
10     int a = 1;
11     double b = 1.2;
12     float c = 1.3;
13     if (i == 0)
14         {
15             cout<<"In C, it is OK"<<endl;
16         }
17     else if(i == 1)
18         {
19             throw a;
20         }
21     else if(i == 2)
22         {
23             throw b;
24         }
25     else
26         {
27             throw c;
28         }
29 }
30 
31 void B (int i)
32 {
33     cout<<"call C ..."<<endl;
34     C(i);
35     cout<<"After call C"<<endl;
36 }
37 void A(int i)
38 {
39     try {
40         B(i);
41     }catch (int j)//只可以捕捉int型的变量,其他的就会使用磨人的
42     {
43         cout<<"catch exception "<<j<<endl;
44     }
45 }
46 int main(int argc,char**argv)
47 {
48     int i;
49     if (argc != 2)
50         {
51             cout<<"Usage: "<<endl;
52             cout<<argv[0]<<"<0|1|2|3>"<<endl;
53             return -1;
54         }
55     i = strtoul(argv[1],NULL,0);
56     A(i);
57     return 0;
58 }
View Code

运行结果:

技术图片

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 void C (int i)
 9 {
10     int a = 1;
11     double b = 1.2;
12     float c = 1.3;
13     if (i == 0)
14         {
15             cout<<"In C, it is OK"<<endl;
16         }
17     else if(i == 1)
18         {
19             throw a;
20         }
21     else if(i == 2)
22         {
23             throw b;
24         }
25     else
26         {
27             throw c;
28         }
29 }
30 
31 void B (int i)
32 {
33     cout<<"call C ..."<<endl;
34     C(i);
35     cout<<"After call C"<<endl;
36 }
37 void A(int i)
38 {
39     try {
40         B(i);
41     }catch (int j)//只可以捕捉int型的变量,其他的就会使用磨人的
42     {
43         cout<<"catch exception "<<j<<endl;
44     }catch(double b)
45     {
46         cout<<"catch double exception"<<b<<endl;
47     }catch(...)//解决了其他情况的时候
48     {
49         cout<<"catch other exception"<<endl;
50     }
51     
52 }
53 int main(int argc,char**argv)
54 {
55     int i;
56     if (argc != 2)
57         {
58             cout<<"Usage: "<<endl;
59             cout<<argv[0]<<"<0|1|2|3>"<<endl;
60             return -1;
61         }
62     i = strtoul(argv[1],NULL,0);
63     A(i);
64     return 0;
65 }
View Code
技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 class MyExveption{
 9 public:
10     void what(void){
11     cout <<"This is MyException"<<endl;
12     }
13 
14 };
15 
16 void C (int i)
17 {
18     int a = 1;
19     double b = 1.2;
20     float c = 1.3;
21     if (i == 0)
22         {
23             cout<<"In C, it is OK"<<endl;
24         }
25     else if(i == 1)
26         {
27             throw a;
28         }
29     else if(i == 2)
30         {
31             throw b;
32         }
33     else if(i == 4)
34         {
35             throw MyExveption();//扔出一个对象
36         }
37     else
38         {
39             throw c;
40         }
41 }
42 
43 void B (int i)
44 {
45     cout<<"call C ..."<<endl;
46     C(i);
47     cout<<"After call C"<<endl;
48 }
49 void A(int i)
50 {
51     try {
52         B(i);
53     }catch (int j)//只可以捕捉int型的变量,其他的就会使用磨人的
54     {
55         cout<<"catch exception "<<j<<endl;
56     }catch(double b)
57     {
58         cout<<"catch double exception"<<b<<endl;
59     }catch(MyExveption &e)
60     {
61         e.what();
62     }catch(...)//解决了其他情况的时候
63     {
64         cout<<"catch other exception"<<endl;
65     }
66     
67 }
68 int main(int argc,char**argv)
69 {
70     int i;
71     if (argc != 2)
72         {
73             cout<<"Usage: "<<endl;
74             cout<<argv[0]<<"<0|1|2|3>"<<endl;
75             return -1;
76         }
77     i = strtoul(argv[1],NULL,0);
78     A(i);
79     return 0;
80 }
View Code

上述代码抛出中有对象

基类函数中没有实现多态:

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 class MyException {
 9 public:
10     void what(void) { cout<<"This is MyException"<<endl; }
11 };
12 
13 class MySubException : public MyException{
14 public:
15     void what(void) { cout<<"This is MySubException"<<endl; }
16 };
17 
18 
19 void C(int i)
20 {
21     int a = 1;
22     double b= 1.2;
23     float c = 1.3;
24 
25     if (i == 0)
26     {
27         cout<<"In C, it is OK"<<endl;
28     }
29     else if (i == 1)
30         throw a;
31     else if (i == 2)
32         throw b;
33     else if (i == 3)
34         throw c;
35     else if (i == 4)
36         throw MyException();
37     else if (i == 5)
38         throw MySubException();
39 }
40 
41 void B(int i)
42 {
43     cout<<"call C ..."<<endl;
44     C(i);
45     cout<<"After call C"<<endl;
46 }
47 
48 void A(int i)
49 {
50     try {
51         B(i);
52     } catch (int j)
53     {
54         cout<<"catch int exception "<<j<<endl;
55     } catch (double d)
56     {
57         cout<<"catch double exception "<<d<<endl;
58     } catch (MyException &e)//由于基类没有实现多态所以只会使用基类的函数
59     {
60         e.what();
61     } catch (...){
62         cout<<"catch other exception "<<endl;
63     }
64 }
65 
66 
67 int main(int argc, char **argv)
68 {
69     int i;
70     if (argc != 2)
71     {
72         cout<<"Usage: "<<endl;
73         cout<<argv[0]<<" <0|1|2|3>"<<endl;
74         return -1;
75     }
76 
77     i = strtoul(argv[1], NULL, 0);
78 
79     A(i);
80     
81     return 0;
82 }
View Code

运行结果:

技术图片

基类中实现多态:

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 class MyException {
 9 public:
10     virtual void what(void) { cout<<"This is MyException"<<endl; }//实现多态
11 };
12 
13 class MySubException : public MyException{
14 public:
15     void what(void) { cout<<"This is MySubException"<<endl; }
16 };
17 
18 
19 void C(int i)
20 {
21     int a = 1;
22     double b= 1.2;
23     float c = 1.3;
24 
25     if (i == 0)
26     {
27         cout<<"In C, it is OK"<<endl;
28     }
29     else if (i == 1)
30         throw a;
31     else if (i == 2)
32         throw b;
33     else if (i == 3)
34         throw c;
35     else if (i == 4)
36         throw MyException();
37     else if (i == 5)
38         throw MySubException();
39 }
40 
41 void B(int i)
42 {
43     cout<<"call C ..."<<endl;
44     C(i);
45     cout<<"After call C"<<endl;
46 }
47 
48 void A(int i)
49 {
50     try {
51         B(i);
52     } catch (int j)
53     {
54         cout<<"catch int exception "<<j<<endl;
55     } catch (double d)
56     {
57         cout<<"catch double exception "<<d<<endl;
58     } catch (MyException &e)//由于基类没有实现多态所以只会使用基类的函数
59     {
60         e.what();
61     } catch (...){
62         cout<<"catch other exception "<<endl;
63     }
64 }
65 
66 
67 int main(int argc, char **argv)
68 {
69     int i;
70     if (argc != 2)
71     {
72         cout<<"Usage: "<<endl;
73         cout<<argv[0]<<" <0|1|2|3>"<<endl;
74         return -1;
75     }
76 
77     i = strtoul(argv[1], NULL, 0);
78 
79     A(i);
80     
81     return 0;
82 }
View Code

运行结果:

技术图片

                       函数B可以声明异常     

                            void B(void) thow (int, double, MyException){ ... } //会抛出int,double,MyException类异常
                            void B(void) thow (){ ... } //不会会抛出任何异常
                            void B(void) { ... } //可能抛出任何异常

 

                                 可以事先设置处理函数

想知道函数B会抛出什么异常-----------------------------如果抛出了意外的异常 

                                 对于意料之外的异常,会执行2个函数:
                                 "unexpected"函数(可以自己提供),
                                 "terminate"函数(可以自己提供)

                                  

                                 

                                 "unexpected"函数用来处理声明之外的异常
                                 "terminate"函数用来处理"catch分支未捉到异常"

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 class MyException {
 9 public:
10     virtual void what(void) { cout<<"This is MyException"<<endl; }
11 };
12 
13 class MySubException : public MyException{
14 public:
15     void what(void) { cout<<"This is MySubException"<<endl; }
16 };
17 
18 
19 void C(int i) throw(int, double)
20 {
21     int a = 1;
22     double b= 1.2;
23     float c = 1.3;
24 
25     if (i == 0)
26     {
27         cout<<"In C, it is OK"<<endl;
28     }
29     else if (i == 1)
30         throw a;
31     else if (i == 2)
32         throw b;
33     else if (i == 3)
34         throw c;
35     else if (i == 4)
36         throw MyException();
37     else if (i == 5)
38         throw MySubException();
39 }
40 
41 void B(int i)
42 {
43     cout<<"call C ..."<<endl;
44     C(i);
45     cout<<"After call C"<<endl;
46 }
47 
48 void A(int i)
49 {
50     try {
51         B(i);
52     } catch (int j)
53     {
54         cout<<"catch int exception "<<j<<endl;
55     } catch (double d)
56     {
57         cout<<"catch double exception "<<d<<endl;
58     } catch (MyException &e)
59     {
60         e.what();
61     } catch (...){
62         cout<<"catch other exception "<<endl;
63     }
64 }
65 #if 1
66 void my_unexpected_func() 
67 {
68     cout<<"my_unexpected_func"<<endl;
69 }
70 
71 
72 void my_terminate_func () { cout<<"my_terminate_func"<<endl; }  
73 
74 #endif
75 int main(int argc, char **argv)
76 {
77     int i;
78 
79     set_unexpected (my_unexpected_func);
80     set_terminate(my_terminate_func);
81     
82     if (argc != 2)
83     {
84         cout<<"Usage: "<<endl;
85         cout<<argv[0]<<" <0|1|2|3>"<<endl;
86         return -1;
87     }
88 
89     i = strtoul(argv[1], NULL, 0);
90 
91     A(i);
92     
93     return 0;
94 }
View Code

运行结果:

技术图片

技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <unistd.h>
 4 #include <stdlib.h>
 5 
 6 using namespace std;
 7 
 8 class MyException {
 9 public:
10     virtual void what(void) { cout<<"This is MyException"<<endl; }
11 };
12 
13 class MySubException : public MyException{
14 public:
15     void what(void) { cout<<"This is MySubException"<<endl; }
16 };
17 
18 
19 void C(int i) throw(int, double)
20 {
21     int a = 1;
22     double b= 1.2;
23     float c = 1.3;
24 
25     if (i == 0)
26     {
27         cout<<"In C, it is OK"<<endl;
28     }
29     else if (i == 1)
30         throw a;
31     else if (i == 2)
32         throw b;
33     else if (i == 3)
34         throw c;
35     else if (i == 4)
36         throw MyException();
37     else if (i == 5)
38         throw MySubException();
39 }
40 
41 void B(int i)
42 {
43     cout<<"call C ..."<<endl;
44     C(i);
45     cout<<"After call C"<<endl;
46 }
47 
48 void A(int i)
49 {
50     try {
51         B(i);
52     } catch (int j)
53     {
54         cout<<"catch int exception "<<j<<endl;
55     } catch (MyException &e)
56     {
57         e.what();
58     } 
59 }
60 
61 void my_unexpected_func() 
62 {
63     cout<<"my_unexpected_func"<<endl;
64 }
65 
66 void my_terminate_func () { cout<<"my_terminate_func"<<endl; }  
67 
68 
69 int main(int argc, char **argv)
70 {
71     int i;
72 
73     set_unexpected (my_unexpected_func);
74     set_terminate(my_terminate_func);
75     
76     if (argc != 2)
77     {
78         cout<<"Usage: "<<endl;
79         cout<<argv[0]<<" <0|1|2|3>"<<endl;
80         return -1;
81     }
82 
83     i = strtoul(argv[1], NULL, 0);
84 
85     A(i);
86     
87     return 0;
88 }
View Code

上述代码用来验证:

"unexpected"函数用来处理声明之外的异常
"terminate"函数用来处理"catch分支未捉到异常"

运行结果:

技术图片

 

  

c++之异常

标签:设置   other   cal   exce   closed   float   his   src   lap   

原文地址:https://www.cnblogs.com/yekongdebeijixing/p/12189719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!