异常—银行
Posted 曦~妍
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异常—银行相关的知识,希望对你有一定的参考价值。
1 package Exception; 2 import java.util.*; 3 public class Bank { 4 /*建立exception包,建立Bank类,类中有变量double balance表示存款, 5 * Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount), 6 * 当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数, 7 * 抛出NagativeFundsException,如new Bank(100),表示存入银行100元, 8 * 当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。*/ 9 private double balance; 10 public double getBalance() { 11 return balance; 12 } 13 14 public void setBalance(double balance) { 15 this.balance = balance; 16 } 17 18 Bank(double balance) 19 { 20 this.balance=balance; 21 } 22 23 double withDrawal(double dAmount) throws Exception 24 { 25 if(balance<dAmount) 26 { 27 throw new Exception("InsufficientFundsException"); 28 } 29 else if(dAmount<0) 30 { 31 throw new Exception("NagativeFundsException"); 32 } 33 return balance-dAmount; 34 } 35 public static void main(String[] args) { 36 // TODO 自动生成的方法存根 37 38 Bank b=new Bank(100); 39 try 40 { 41 b.withDrawal(150); 42 } 43 catch(Exception ex) 44 { 45 ex.printStackTrace(); 46 } 47 try 48 { 49 b.withDrawal(-15); 50 } 51 catch(Exception ex) 52 { 53 ex.printStackTrace(); 54 } 55 56 } 57 58 }
以上是关于异常—银行的主要内容,如果未能解决你的问题,请参考以下文章
mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段
mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段
springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段