上級 › unreported exception must be caught の原因と直し方 › パターン4
unreported exception must be caught の原因と直し方
検査例外と非検査例外の境界。
1 public class Main { 2 public static void main(String[] args) { 3 try { 4 throw new java.io.FileNotFoundException("x"); 5 } catch (____________________ e) { ^ 6 System.out.println("io"); 7 } catch (java.io.FileNotFoundException e) { 8 System.out.println("fnf"); 9 } 10 } 11 }
Main.java:7: error: exception FileNotFoundException has already been caught
空欄を埋めてビルドを通す
FileNotFoundExceptionはIOExceptionのサブクラスです。先にIOExceptionで捕まえると、後のcatchは絶対に実行されないため、コンパイルエラーになります。
次の問題