<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                在 Java 中你可以自定義異常。編寫自己的異常類時需要記住下面的幾點。 * 所有異常都必須是 Throwable 的子類。 * 如果希望寫一個檢查性異常類,則需要繼承 Exception 類。 * 如果你想寫一個運行時異常類,那么需要繼承 RuntimeException 類。 可以像下面這樣定義自己的異常類: ``` class MyException extends Exception{ } ``` 只繼承Exception 類來創建的異常類是檢查性異常類。 下面的 InsufficientFundsException 類是用戶定義的異常類,它繼承自 Exception。 一個異常類和其它任何類一樣,包含有變量和方法。 ### 實例 以下實例是一個銀行賬戶的模擬,通過銀行卡的號碼完成識別,可以進行存錢和取錢的操作。 ``` package day02try; import java.io.*; // 自定義異常類,繼承Exception類 public class day08Exception extends Exception{ private double amount; public day08Exception(double amount){ this.amount = amount; } public double getAmount(){ return amount; } } ``` 為了展示如何使用我們自定義的異常類, 在下面的 CheckingAccount 類中包含一個 withdraw() 方法拋出一個 InsufficientFundsException 異常。 ``` import java.io.*; //此類模擬銀行賬戶 public class CheckingAccount { //balance為余額,number為卡號 private double balance; private int number; public CheckingAccount(int number) { this.number = number; } //方法:存錢 public void deposit(double amount) { balance += amount; } //方法:取錢 public void withdraw(double amount) throws InsufficientFundsException { if(amount <= balance) { balance -= amount; } else { double needs = amount - balance; throw new InsufficientFundsException(needs); } } //方法:返回余額 public double getBalance() { return balance; } //方法:返回卡號 public int getNumber() { return number; } } ``` 下面的 BankDemo 程序示范了如何調用 CheckingAccount 類的 deposit() 和 withdraw() 方法。 ``` public class BankDemo { public static void main(String [] args) { CheckingAccount c = new CheckingAccount(101); System.out.println("Depositing $500..."); c.deposit(500.00); try { System.out.println("\nWithdrawing $100..."); c.withdraw(100.00); System.out.println("\nWithdrawing $600..."); c.withdraw(600.00); }catch(InsufficientFundsException e) { System.out.println("Sorry, but you are short $" + e.getAmount()); e.printStackTrace(); } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看