问题描述
importjava.util.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;/***银行类**/classBank{privatedoublebalance;//余额Bank(doubleb){balance=b;}publicvoiddeposite(doubledAmount)//存钱{if(dAmount>0.0)balance+=dAmount;JOptionPane.showMessageDialog(null,"你当前存了"+dAmount+",现有余额:"+this.getBalance());}publicvoidwithdrawal(doubledAmount)throwsFundsException//取钱{if(balance<dAmount){thrownewFundsException(this,dAmount);}balance=balance-dAmount;JOptionPane.showMessageDialog(null,"你当前取了"+dAmount+",现有余额:"+this.getBalance());}publicdoublegetBalance()//获取余额{returnbalance;}}/***自定义异常类**/classFundsExceptionextendsException{privateBankexcepbank;privatedoubleexcepAmount;FundsException(Bankba,doubledAmount){excepbank=ba;excepAmount=dAmount;}publicStringexcepMesagge(){Stringstr="现有余额:"+excepbank.getBalance()+",而你要取:"+excepAmount+",超出了取款范围!";returnstr;}}/***主类**/publicclassExceptionDefDemo{publicstaticvoidmain(String[]args){Bankbank=newBank(50);bank.withdrawal(100);}}此程序有何错误,错误如下:FundsExceptionatBank.withdrawal(ExceptionDefDemo.java:28)atExceptionDefDemo.main(ExceptionDefDemo.java:76)
解决方案
解决方案二:
银行有50存款,要取100出来,所以抛出异常了。