程式執行時,常見的例外錯誤
1. 存取陣列時,超出陣列的索引範圍 2. 數學運算時,除數為 0 3. 執行時,開啟的檔案不存在 4. 載入的檔案找不到或格式不對 5. 嘗試參考 null 6. 嘗試執行違反安全的操作1. 存取陣列時,超出陣列的索引範圍
public class ExceptionArray {
 public static void main(String[] args) {
  int[] array = new int[5];
  array[10] = 1;
  System.out.println("執行結束");
 }
}
執行結果
2. 數學運算時,除數為 0
public class ExceptionZero {
 public static void main(String[] args) {
  int a = 5;
  int b = 0;
  System.out.println("Answer: " + a/b);
 }
}
執行結果


沒有留言:
張貼留言