上級 › unreported exception must be caught の原因と直し方 › パターン5
unreported exception must be caught の原因と直し方
検査例外と非検査例外の境界。
1 import java.io.*; 2 3 public class Main { 4 static class MyRes implements Closeable { 5 public void close() throws IOException { throw new IOException("x"); } 6 } 7 public static void main(String[] args)____________________ { ^ 8 try (MyRes r = new MyRes()) { 9 System.out.println("ok"); 10 } 11 } 12 }
Main.java:7: error: unreported exception IOException; must be caught or declared to be thrown
空欄を埋めてビルドを通す
try-with-resourcesはブロックを抜けるときに自動でclose()を呼びます。close()が投げる検査例外も、通常のthrowと同じく宣言や捕捉が必要です。
次の問題