2016/5/27 1004. 简单减法
来源:互联网 发布:数据黑产吧 编辑:程序博客网 时间:2024/06/05 22:48
和之前的几道题类似,只要在test里try 去调用calc,并catch 它throw出来的异常就好了。这里需要注意的是在函数名后加throw(类型)表示告诉try语句这个函数语句块里throw出来的只会是这种类型的错误。所以如果加的是throw(),就相当于是保证不会出错,不可能throw出错误来了。
#include <iostream>#include<stdexcept>using namespace std;int calc(int a, int b) throw(logic_error){if (a<0) throw out_of_range("Out of range exeception");else if (b<0) throw out_of_range("Out of range exeception");else if (a<b) throw logic_error("Minuend smaller than subtrahend");return a - b;}void test(int a, int b){try{cout << calc(a, b) << endl;}catch (logic_error ex){cout << ex.what() << endl;}}void f(){test(3, 1);test(-3, 1);test(1, -3);test(1, 3);}int main(){f();return 0;}
0 0
- 2016/5/27 1004. 简单减法
- 简单高精度减法
- 减法
- 一个数学学习工具(10之内的简单减法)
- 线性代数·矩阵的加法减法乘法运算简单实现
- 极简设计并不是简单的做减法
- 极简主义风格不是简单的做减法
- 5、大数,高精度计算---大数减法
- 高精度减法
- 高精度减法
- 大数减法
- 大数减法
- 背景减法
- 减法生活
- 高精度减法
- 大数减法
- 大数减法
- 高精度减法
- 安装win7、windows server 2012 出现无法配置在此计算机硬件上运行 解决方案
- 初学java反射机制
- 使用logstash+elasticsearch+kibana快速搭建日志平台
- 1.2.21 Map.values方法——获取Map集合中的所有键值对象
- SpringMVC开发笔记
- 2016/5/27 1004. 简单减法
- Qt之自定义布局管理器(QCardLayout)
- 通过命令的方式实现计算机定时关机
- sas:主成分分析
- 自制Camera例子——相机效果
- 问题:In IOS8 wakeups limit
- Solr -- 查询语法/参数
- js一键复制,Clipboard.js:不用Flash实现剪贴板功能的轻量级JavaScript库
- 修改postgres时区