上級 › finally の return が結果を上書きする問題 › パターン2
finally の return が結果を上書きする問題
finallyでreturnを書いてはいけない理由と、try-with-resourcesによる安全なリソース解放を扱います。
1 import java.io.*; 2 3 public class Main { 4 public static void main(String[] args) throws IOException { 5 try (______________ r = new BufferedReader(new StringReader("a"))) { ^ 6 System.out.println(r.readLine()); 7 } 8 } 9 }
Main.java:5: error: incompatible types: BufferedReader cannot be converted to String
空欄を埋めてビルドを通す
try-with-resourcesの変数はAutoCloseableを実装した型で宣言します。ブロックを抜けると自動でcloseされます。
次の問題