Java 17 Silver ラボ

学習の画面へ

close と catch・finally の順番

出力はどれか。

public class Main {
    public static void main(String[] args) {
        try (Res r = new Res()) {
            System.out.print("try ");
            throw new IllegalStateException();
        } catch (IllegalStateException e) {
            System.out.print("catch ");
        } finally {
            System.out.print("finally");
        }
    }
}

class Res implements AutoCloseable {
    public void close() { System.out.print("close "); }
}
  1. A try close catch finally
  2. B try catch close finally
  3. C try catch finally close
  4. D コンパイルエラーになる
正解と解説を見る

正解: A

ポイント

「try-with-resources 文による例外処理」をこのサイトで解く (記録・間違えた問題の解き直し・AI教師への質問)