簡體   English   中英

使用def異常無法轉換為拋出錯誤

[英]use def exception cannot be converted to throwable error

public class Odexception extends Exception {
     double deficit;
     Odexception(String msg,double def) {
         super(msg);
         this.deficit=def;
     }
}


public   void withdraw(double amt)  throws odexception {
     if(amt<=balance) {
         balance=balance-amt;
     } else {
          throw new Odexception("Insufficient balance",amt-balance);
     }
}

public static void main(string ar[]) {
     try{
        c[0].acc[0].withdraw(2000);
     } catch(Odexception  e) {
        System.out.println(e.getMessage());
     }
}

這是你想要的嗎?

class Odexception extends Exception{
    double deficit;
    Odexception(String msg,double def) {
        super(msg);
        this.deficit=def;
    }
}

public class CustomException {
    double balance;
    public CustomException(double balance)
    {
        this.balance = balance;
    }

    public void withdraw(double amt) throws Odexception {
        if (amt <= balance) {
            {
                balance = balance - amt;
                System.out.println("Withdrawn : "+amt+"\nCurrent Balance is :"+balance);
            }
        } else {
            throw new Odexception("Insufficient balance", amt - balance);
        }
    }

    public static void main(String ar[]) {
        try {
            new CustomException(1000).withdraw(5000);
        } catch (Odexception e) {
            System.out.println(e.getMessage());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM