关于java自定义的问题
小弟,最近学习java自定义异常,就是搞不明白怎么回事?希望那位高手给我编写个示例看看,最好是存款的示例,当存款为负数时候,给出异常,在此我先谢过了……
class balanceInsufficientException extends Exception {
public balanceInsufficientException() {
super("余额不足");
}
}
class CashWithdraw {
private double money = 100.0;
public void withdraw(double value) throws balanceInsufficientException {
if((money - value)<0) {
throw new balanceInsufficientException();
}
money -= value;
}
}
public class Test {
public static void main(String... a) throws Exception {
CashWithdraw cw = new CashWithdraw();
cw. withdraw(50);
cw. withdraw(60);
}
}