Try 與 Catch

Java 的資料型別與例外狀況

Jim White

Java Developer

Java 中的問題

  • 應用程式可能出現問題或狀況
    • 來自錯誤的輸入
    • 來自程式碼錯誤
    • 來自不可預期情況
  • 這些問題稱為 _例外_
    • 會中斷正常控制流程
  • Java 會 _拋出_ 例外
Java 的資料型別與例外狀況

例外範例

public static void main(String[] args) {
    ArrayList<String> allStars = new ArrayList<String>();
    allStars.add("Mays");
    allStars.add("Aaron");
    allStars.add("Ruth");
    String last = allStars.get(3); // This will cause an exception
    System.out.println(last);
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of 
bounds for length 3
Java 的資料型別與例外狀況

Try

  • 用 try 區塊包住可能產生例外的程式碼
    • 「先嘗試這段程式碼,若有問題就交給 catch 區塊」
try {
    // 要執行並監看是否拋出例外的敘述
}
Java 的資料型別與例外狀況

Catch

  • 在 try 之後放置 catch 區塊
    • 作為處理例外問題的 處理器
    • 被捕捉的 _例外_ 會以參數傳入此區塊
  • 某些例外型別必須使用 try/catch 區塊
    • 其他則為選用
try {
    // 要執行並監看是否拋出例外的敘述
} catch (<Exception type> <variable name>) {
    // 發生例外時要執行的敘述
}
Java 的資料型別與例外狀況

捕捉 IndexOutOfBoundsException

try {
  ArrayList<String> allStars = new ArrayList<String>();
  allStars.add("Mays");
  allStars.add("Aaron");
  allStars.add("Ruth");
  String last = allStars.get(3); // Accessing an element that isn't there
  System.out.println(last);
} catch (IndexOutOfBoundsException e) {
  System.out.println("Oops - wrong index");
}
Oops - wrong index
Java 的資料型別與例外狀況

多重 Catch

  • 使用多個 catch 區塊處理不同型別的例外
  • 每個 catch 都有各自的例外型別與例外變數
  • 發生例外時,Java 會依序檢查 catch 區塊,直到找到可匹配者
try {
    ArrayList<String> nums 
      = new ArrayList<String>();
    nums.add("one");
    String last = nums.get(0);
    Integer.valueOf(last);
    System.out.println(last);
} catch (IndexOutOfBoundsException eIndex) {
    System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
    System.out.println(
      "Oops - String not a number");
}
Oops - String not a number
Java 的資料型別與例外狀況

Catch「總攬」

  • 使用 catch (Exception variable){ } 作為總攬式捕捉
    • 處理其他 catch 區塊未處理的任何例外
try {
    // 要嘗試的程式碼
} catch (IndexOutOfBoundsException eIndex) {
    System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops - String not a number");
} catch (Exception e) { // 處理上述兩者以外的所有例外
    System.out.println("發生未預期的事情");
}
Java 的資料型別與例外狀況

Finally

  • finally 區塊中的程式碼一定會執行
// 發生例外時
try {
    Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops");
} finally {
    System.out.println("Doing cleanup");
}
Oops
Doing cleanup

 

// 未拋出例外時
try {
    Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops");
} finally {
    System.out.println("Doing cleanup");
}
Doing cleanup
Java 的資料型別與例外狀況

Exception 物件

  • 傳入 catch 區塊的 Exception 物件包含豐富資訊
  • 呼叫其方法了解問題細節
    • 使用 .getMessage() 取得人類可讀的原因
    • 使用 .getClass() 取得例外的類別或型別名稱
    • 使用 .printStackTrace() 顯示堆疊追蹤(下一張)
public class ExceptionExample {

    public static void main(String[] args) {
        try {
            Integer.valueOf("one");
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
Java 的資料型別與例外狀況

例外的堆疊追蹤

  • 堆疊追蹤 提供通往問題的執行流程紀錄
    • 下方:前一個程式碼範例的輸出,包含列印出的 堆疊追蹤
For input string: "one"
java.lang.NumberFormatException: For input string: "one"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:661)
    at java.base/java.lang.Integer.valueOf(Integer.java:988)
    at foo/exceptions.ExceptionExample.main(ExceptionExample.java:7)
Java 的資料型別與例外狀況

一起來練習吧!

Java 的資料型別與例外狀況

Preparing Video For Download...