?? bank.java
字號:
//余額不足異常
class BalanceLackException extends Exception
{
public String toString()
{
return "BalanceLackException:余額不足!";
}
}
public class Bank
{
public int balance; //余額
private static boolean exceptionSign; //異常標志,用于判斷是否有異常產生
public Bank(int balance)
{
this.balance=balance;
this.exceptionSign=false;
}
public int getBalance ()
{
return this.balance;
}
public void fetchFund(int money) throws
BalanceLackException{
if (money>balance) throw new BalanceLackException(); //引發余額不足異常
balance -= money;
}
public static void main(String[] args)
{
Bank bank =new Bank(1000);
try{
bank.fetchFund(500);
bank.fetchFund(1000);
bank.fetchFund(2500);
}
catch(BalanceLackException e)
{
exceptionSign = true;
System.out.println(e);
}
finally{
if (exceptionSign) System.out.println("有錯誤產生,操作未完成!");
else System.out.println("操作完成!目前的余額為:"+bank.getBalance());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -