# 例外處理 EXCEPTION ## ExException 分為RuntimeException與其他Exception ### RuntimeException:不作try catch ,compiler不會抓你錯 1. ArrayIndexOutOfBoundsException 陣列超過存取所引值 2. IllegalArgumentException 參數傳入錯誤 3. NullPointerException 操作空的物件 4. ArithmeticException 計算/0錯誤 ### 其他Exception(Checked Exception):一定要做try catch ,不然compiler不會過 1. 法1.透過try catch 處理 2. 法2.透過方法宣告throws +錯誤類別 ,把Exception把往外拋,「由呼叫者」處理,(如果是main方法,由JVM處理)。 4. 若有多種Exception要處理,可由逗號隔開。 ``` import java.io.FileNotFoundException; import java.io.FileReader; public class Selftest { public static void main(String[] args) throws FileNotFoundException,ZipException { FileReader fr= new FileReader("C:\\java\\.aaa.txt"); System.out.println("done"); } } ```