Java上机课自定义异常类练习
class IFException extends Exception{
private Account account;//账户
private double dAmount;//取款金额
IFException(){}
IFException(Account account,double dAmount){
this.account=account;
this.dAmount=dAmount;
}
public String getMessage(){
String Info;
Info="账户余额为"+account.getbalance()+"取钱额为"+dAmount+",余额不足!";
return Info;
}
}
class Account{
double balance;//账户余额
public void cunqian(double dAmount){
if(dAmount>0)
balance+=dAmount;
}
public void quqian(double dAmount)throws IFException{
if(dAmount>balance)
throw new IFException(this,dAmount);
else balance=balance-dAmount;
}
double getbalance(){
return balance;
}
}
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Account account=new Account();
account.cunqian(1000);
account.quqian(2000);
}catch(IFException e){
e.getMessage();
}
}
}
Notice
参考:
Java语言程序设计实用教程.中国华电出版社