首页 > 代码库 > java异常处理:建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),

java异常处理:建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),

建立exception包,建立Bank类,类中有变量double  balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。

package exception;public class Bank extends Exception{        static double balance=0;     Bank(double r)    {        balance+=r;    }        void withDrawal(double dAmount) throws  InsufficientFundsException,NagativeFundsException{             if(dAmount > balance) throw new InsufficientFundsException("取款余额不足");             else  if(dAmount<0) throw new NagativeFundsException("取款为负数");             else       System.out.print("银行里还剩余:"+(balance-dAmount));    }}
package exception;public class InsufficientFundsException extends Exception {    String s1;     InsufficientFundsException(String t)    {   s1=t;  }       public String getMassage1()    {    return s1;   }    } 
package exception;public class NagativeFundsException extends Exception {        String s;     NagativeFundsException(String t)    {   s=t;  }       public String getMassage()    {    return s;   }    }

技术分享

 

java异常处理:建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),