There are libraries that provide safe arithmetic operations, which check integer overflow/underflow . For example, Guava's IntMath.checkedAdd(int a, int b) returns the sum of a
and b
, provided it does not overflow, and throws ArithmeticException
if a + b
overflows in signed int
arithmetic.