抛出异常
2015-11-14 10:42
190 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.Write("请输入你要进行的运算及数(+):"); string f = Console.ReadLine(); Console.Write("请输入第一个数:"); int a = int.Parse(Console.ReadLine()); Console.Write("请输入第二个数:"); int b = int.Parse(Console.ReadLine()); Console.Write("请输入答案:"); int y = int.Parse(Console.ReadLine()); Calldess dss=new Calldess(); try { dss.Execute(f, a, b); throw new DivideByZeroException("尝试除以零吗?"); } catch (DivideByZeroException e) { if (f == "/") { if (b == 0) { Console.WriteLine(e.Message); } } } Console.Read(); } } public class Calldess { int sun; public int Execute(string op, int a, int b) { switch (op) { case "+": sun = a + b; break; case "-": sun = a - b; break; case "*": sun = a * b; break; case "/": if (b == 0) { //Console.WriteLine("除数不能为零!"); } else { sun = a / b; } break; } return sun; } } }
<总结>
感觉这样写还是有点怪怪的。
相关文章推荐
- HDU 5441 Travel
- 适配器模式
- 适配器模式
- 英语单词常用前缀(1-20)
- java项目打成jar包时引用了第三方jar,此时我们该如何解决呢
- Python编写微信打飞机小游戏(七)
- android OTA更新
- 信息熵
- Git/GitLab简介和基本使用方法
- 文件被explorer.exe占用,不能完全删除
- ConnectivityManager(三)
- Linux的时间设置与同步(NTP)
- Could not create the view: An unexpected exception was thrown.问题解决
- mysql 远程root赋予创建用户权限
- 更新 Ubuntu 系统,避免报错「hash校验和不符」
- 黑马程序员_JavaSE基础知识总结二十:课堂上关于集合的几个超经典案例(结课前集合知识温故)
- Git教程
- 类之间的关系:泛化、依赖、关联、聚合、合成
- spark介绍
- 铁轨(栈)