unreported exception must be caught の原因と直し方
検査例外と非検査例外の境界。throwとthrowsの違い、catchできない型の条件を扱います。
エラーメッセージの読み方
Main.java:5: error: unreported exception IOException;
Main.java- ファイル名
5- 行番号 — javacが異常に気づいた位置。原因の行とは限りません
error- 種別 — コンパイルエラー。実行まで到達しません
unreported exception IOException;- 内容 — javacが期待していたもの、または見つけられなかったもの
このエラーが出る典型パターン
パターン1
1 import java.io.IOException; 2 3 public class Main { 4 public static void main(String[] args) { ^ 5 throw new IOException("boom"); 6 } 7 }
Main.java:5: error: unreported exception IOException;
must be caught or declared to be thrown
検査例外はtry-catchで捕まえるか、throwsで宣言する必要があります。throwは動作、throwsは宣言です。
直し方: (空) を throws IOException にします。
広告
広告スロット(未設定)
パターン2
1 public class Main { 2 public static void main(String[] args) { 3 try { 4 Thread.sleep(10); 5 } catches (InterruptedException e) { ^ 6 System.out.println("interrupted"); 7 } 8 } 9 }
Main.java:5: error: ';' expected
例外を受け取るブロックはcatchです。Javaにexceptというキーワードはありません。
直し方: catches を catch にします。
パターン3
1 import java.io.IOException; 2 3 public class Main { 4 public static void main(String[] args) { 5 try { 6 int[] a = new int[1]; 7 System.out.println(a[5]); 8 } catch (IOException e) { ^ 9 System.out.println("caught"); 10 } 11 } 12 }
Main.java:8: error: exception IOException is never thrown in body of corresponding try statement
try内で発生しない検査例外は捕まえられません。非検査例外なら書けますが、型が合わなければ捕捉されません。
直し方: IOException を ArrayIndexOutOfBoundsException にします。
よくある誤解
catchして握りつぶすのは「対処」ではありません。その場で対処できないなら、throwsで上に投げるほうが正しい場面が多いです。
まとめ
unreported exception must be caughtは上級でつまずきやすい項目です。上の3パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。