Java : ArithmeticException (算術例外) - API使用例
ArithmeticException (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
算術計算で例外的条件が発生した場合にスローされます。 たとえば、整数を「ゼロで除算」するとこのクラスのインスタンスがスローされます。
非チェック例外である ArithmeticException は、ゼロ除算などの算術計算で問題があると発生します。
try {
final var ret = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException! : " + e.getMessage());
}
// 結果
// ↓
//ArithmeticException! : / by zero
try {
final var ret = Math.addExact(Integer.MAX_VALUE, 1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException! : " + e.getMessage());
}
// 結果
// ↓
//ArithmeticException! : integer overflow
コンストラクタ
ArithmeticException ()
詳細メッセージなしでArithmeticExceptionを構築します。
final var e = new ArithmeticException();
System.out.println(e); // java.lang.ArithmeticException
ArithmeticException (String s)
指定された詳細メッセージを持つArithmeticExceptionを構築します。
final var e = new ArithmeticException("abcde");
System.out.println(e); // java.lang.ArithmeticException: abcde
System.out.println(e.getMessage()); // abcde
Throwableで宣言されたメソッド
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
「Java API 使用例 : Throwable」をご参照ください。