您的位置:首页 > 编程语言 > Java开发

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

2017-03-03 13:50 411 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: