如何自定义异常?
public class BalanceNotEnoughException extends Exception{ public BalanceNotEnoughException() {
super();
// TODO Auto-generated constructor stub
} public BalanceNotEnoughException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
} public BalanceNotEnoughException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
} public BalanceNotEnoughException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

}
public Account userWithdraw(long id,double money){
for(int i=0;i<count;i++){
if(account[i].getId()==id){
try{
if(account[i].getAccountType()==0 && account[i].getBalance()<money){
throw new BalanceNotEnoughException("您的储蓄账户余额不足,不能取款!");
}else if(account[i].getAccountType()==1 && ((CreditAccount)account[i]).getOverdraftMoney()<account[i].getBalance()-money){
throw new BalanceNotEnoughException("您的透支余额不足,不能取款!");
}
account[i].withdraw(money);
System.out.println(account[i].getUserName()+"取款成功!");
}catch(BalanceNotEnoughException bnee){

}

}
}
大概意思是这样的,我做的是一个BAM,银行账户管理系统,第二段代码是我其中的一个userWithdraw()取款的方法,现在我想自己定义一个异常BalanceNotEnoughException() ,也就是我第一段代码中的.如果我的取款的时候取得数目超出了我的余额,那么我就要抛出异常!account[i].getAccountType()==0的意思我我的存储账户有两种,一种是普通账户,用0代表,一种是信用账户,用1代表!if(account[i].getAccountType()==0 && account[i].getBalance()<money)的意思是如果我是一个普通账户,并且我的取款数目超出我的余额,那么就抛出异常
if(account[i].getAccountType()==1 && ((CreditAccount)account[i]).getOverdraftMoney()()<account[i].getBalance()-money)的意思是如果我是一个信用账户,并且我的取款数目超出了我的透支余额最大限度,也抛出异常!大概意思就是这样,我写的这两段代码是没有实现我想要的功能,谁能告诉我如何实现?

解决方案 »

  1.   

    参考jdk源代码,比如String类,里面有很多异常
      

  2.   


    public class PasswordError extends RuntimeException{    public PasswordError() {
        }
        
        
    }
      

  3.   

    是这样的:
    public class PasswordError extends RuntimeException{    public PasswordError() {
        }
        
        
    }
      

  4.   


    public class PasswordError extends RuntimeException{    public PasswordError() {
        }
    }
    然后选择会出现异常的方法抛出异常
      

  5.   

    不明白你的没实现你的功能是什么意思,代码看上去没什么问题.
    建议你不要自己catch了,在方法后边加上throws吧,让别人去catch.
    现在你自己catch,又没做任何处理...
      

  6.   

    5楼说得对,你没有必要把它catch住,让最后View层去抓住显示出来就可以了。
      

  7.   

    public Account userWithdraw(long id,double money){
            for(int i=0;i<count;i++){
                if(account[i].getId()==id){
                    try{
                        if(account[i].getAccountType()==0 && account[i].getBalance()<money){                        throw new BalanceNotEnoughException("您的储蓄账户余额不足,不能取款!");
                        }else if(account[i].getAccountType()==1 && ((CreditAccount)account[i]).getOverdraftMoney()<account[i].getBalance()-money){
                            throw new BalanceNotEnoughException("您的透支余额不足,不能取款!");
                        }
                        account[i].withdraw(money);
                        System.out.println(account[i].getUserName()+"取款成功!");
                    }catch(BalanceNotEnoughException bnee){
                        System.out.println("取款失败!"+bnee.getMessages());//处理异常
                    }
                    
                }
            }
      

  8.   

    在API没有这个异常或者我们想对某个异常更精确的描述的话,我们就会用到自定义异常,而且这样的自定义异常大都为受检查异常,下面就是一个自定义异常的例子:public class CarException extends Exception {  public CarException() {
      }  public CarException(String message) {
        super(message);
      }  public CarException(Throwable cause) {
        super(cause);
      }  public CarException(String message, Throwable cause) {
        super(message, cause);
      }}
      

  9.   

    自定义异常例子:
    class MyException extends Exception{
       void f(){
         System.out.println("num<0,error!,注册失败!");
       }
    }class Test{  
      public static void regist(int num) throws MyException{
          if(num<0) throw new MyException();
          else System.out.println("注册成功!");
       }
      public static void main(String[] args){
        try{
             regist(-1);
        }catch(MyException e){
          e.f();
        }
    }