首页 > 编程 > Java > 正文

java创建银行账户,自定义取钱超过余额异常

2019-11-06 08:16:51
字体:
来源:转载
供稿:网友
public class Account { int money; public Account(int money) { this.money = money; } public void withdraw(int amount) throws InsufficientFundsException { if (amount > money) { throw new InsufficientFundsException("余额不足!" + "取款额度:" + amount + "实际额度:" + money); } money -= amount; }} class Client { public static void main(String[] args) { Account a = new Account(500); try { a.withdraw(1000); } catch (InsufficientFundsException e) { e.PRintStackTrace(); } }}class InsufficientFundsException extends Exception { public InsufficientFundsException(String s) { super(s); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表