Java 17 Silver ラボ

学習の画面へ

原因の例外を持たせる

出力はどれか。

public class Main {
    public static void main(String[] args) {
        try {
            try {
                int x = 1 / 0;
            } catch (ArithmeticException e) {
                throw new CalcException("計算失敗", e);
            }
        } catch (CalcException e) {
            System.out.println(e.getMessage() + " / " + e.getCause().getClass().getSimpleName());
        }
    }
}

class CalcException extends RuntimeException {
    CalcException(String message, Throwable cause) {
        super(message, cause);
    }
}
  1. A 計算失敗 / ArithmeticException
  2. B 計算失敗 / CalcException
  3. C / by zero / ArithmeticException
  4. D コンパイルエラーになる
正解と解説を見る

正解: A

ポイント

「カスタム例外の作成」をこのサイトで解く (記録・間違えた問題の解き直し・AI教師への質問)