复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33首先通过一个简单的例子来熟悉C++ 的 try/catch/throw(可根据单步调试来熟悉,try catch throw部分是如何运行的): 复制代码 #include <stdlib.h> #include "iostream" using namespace std; double fuc(double x, double y) //定义函数 { if(y==0) { throw y; //除数为0,抛出异常 } return x/y; //否则返回两个数的商 } int _tmain(int argc, _TCHAR* argv[]) { double res; try //定义异常 { res=fuc(2,3); cout<<"The result of x/y is : "<<res<<endl; res=fuc(4,0); //出现异常 } catch(double) //捕获并处理异常 { cerr<<"error of dividing zero.n"; exit(1); //异常退出程序 } return 0; }
c++中try catch的用法 - 超酷小子 - 博客园
最后
以上就是阳光板凳最近收集整理的关于C++ 的 try/catch/throw的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复