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

C++中的异常

时间:2018-03-11 22:32:37      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:pre   log   ble   return   int   span   row   double   pac   

1.异常可以跨越函数 ;2.异常中的catch()严格进行类型匹配;3.异常出现后,可以不处理,再次抛出异常

 1 #include<iostream>
 2 using namespace std;
 3 
 4 
 5 double myderive(int x,int y)
 6 {
 7     try
 8     {
 9        if(y==0)
10        {
11            throw x;
12        }
13 
14        return x/y;
15     }
16 
17     
18     catch(int x)
19     {
20         cout<<x<<"被0除"<<endl;
21     }
22     
23 
24     catch(...)//处理不知道什么原因的异常
25     {
26         cout<<"程序出现未知的异常"<<endl;
27         throw x;
28     }
29 
30 }
31 
32 
33 int main()
34 {
35     myderive(10,0);
36 
37     return 0;
38 }

 

C++中的异常

标签:pre   log   ble   return   int   span   row   double   pac   

原文地址:https://www.cnblogs.com/jswu-ustc/p/8546273.html

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