Java : ArithmeticException with Examples
ArithmeticException (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ArithmeticException methods.
Summary
Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.
try {
final var ret = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException! : " + e.getMessage());
}
// Result
// ↓
//ArithmeticException! : / by zero
try {
final var ret = Math.addExact(Integer.MAX_VALUE, 1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException! : " + e.getMessage());
}
// Result
// ↓
//ArithmeticException! : integer overflow
Constructors
ArithmeticException ()
Constructs an ArithmeticException with no detail message.
final var e = new ArithmeticException();
System.out.println(e); // java.lang.ArithmeticException
ArithmeticException (String s)
Constructs an ArithmeticException with the specified detail message.
final var e = new ArithmeticException("abcde");
System.out.println(e); // java.lang.ArithmeticException: abcde
System.out.println(e.getMessage()); // abcde
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Related posts
- API Examples
- Error
- Exception
- RuntimeException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- CancellationException
- ClassCastException
- ConcurrentModificationException
- DateTimeException
- DateTimeParseException
- IllegalArgumentException
- IllegalStateException
- IndexOutOfBoundsException
- NoSuchElementException
- NullPointerException
- PatternSyntaxException
- StringIndexOutOfBoundsException
- UnsupportedOperationException
- Throwable