133.throw机制 抛出类类型

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //try尝试执行,抛出throw,throw之后语句不再执行
 5 //catch处理throw的异常
 6 
 7 void main()
 8 {
 9     float fl1, fl2;
10     cin >> fl1 >> fl2;
11 
12     //尝试执行,抛出类型检测
13     try
14     {
15         if (fl2 < 0.0000001)
16         {
17             throw 1;
18         }
19         else if (fl1 < 0.000001)
20         {
21             throw 2;
22         }
23 
24         float fl3 = fl1 / fl2;
25         cout << fl3 << endl;
26     }
27     //定义类型,检测判断是什么错误
28     catch (int code)
29     {
30         if (code == 1)
31         {
32             cout << "fl2太小" << endl;
33         }
34         else if (code == 2)
35         {
36             cout << "fl2太小" << endl;
37         }
38     }
39 
40     
41     cin.get();
42     cin.get();
43 }

 

posted @ 2018-04-01 00:16  喵小喵~  阅读(248)  评论(0编辑  收藏  举报