异常3
2016-05-28 13:26
369 查看
建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能初始化余额,Bank类中有存款的方法cunKuan(double balance),取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。
运行:
public class Bank { double yu_e; double balance; Bank(double yu_e) { this.yu_e = yu_e; System.out.println("账户内余额:"+this.yu_e+"元"); } void cunKuan(double balance) throws Exception { if(balance<0){ throw new Exception("存款不能为负"); } yu_e += balance; } void withDrawal(double dAmount) throws Exception { if(dAmount>yu_e){ throw new Exception("InsufficientFundsException,余额不足"); }else if (dAmount<0){ throw new Exception("NagativeFundsException,取款值为负"); } yu_e -= dAmount; } public static void main(String[] args) { Bank b = new Bank(10); try { b.cunKuan(-100); } catch (Exception e) { e.printStackTrace(); } try { b.withDrawal(150); } catch (Exception e) { e.printStackTrace(); } try { b.withDrawal(-15); } catch (Exception e) { e.printStackTrace(); } System.out.println("存款余额"+b.yu_e+"元"); } }
运行:
相关文章推荐
- linux进程间通信之无名管道通信学习笔记
- Python-Week2-Homework
- vertica-创建超级用户
- 安装oh my zsh
- spring(2)装配Bean
- 如何简单形象又有趣地讲解神经网络是什么?
- 学习进度条12
- 使用Python 进行socket编程
- 给kali linux2.0装一个中文输入法
- 没有谁是一座孤岛——《岛上书店》
- hibernate和mybatis思想,区别,优缺点
- iOS 开发遇到问题
- 漏洞挖掘方法之静态扫描+经典栈溢出实例
- Unity3D添加天空盒子
- MLlib分类算法实战演练--Spark学习(机器学习)
- 设计模式
- 第14周项目-排序函数模板
- 测试
- 1601 - The Morning after Halloween
- PHP审计-RIPS