Dividing two BigDecimal values without specifying a scale and rounding mode throws ArithmeticException whenever the exact quotient has no finite decimal representation; supplying a scale and RoundingMode avoids it.
BigDecimal a = new BigDecimal("10");
BigDecimal b = new BigDecimal("3");
try {
BigDecimal result = a.divide(b);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
BigDecimal safe = a.divide(b, 2, RoundingMode.HALF_UP);
System.out.println("With scale: " + safe);
Caught: ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
With scale: 3.33
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27