是的,从Java-8开始,您可以使用新的Exact方法,在溢出时会抛出异常(java.lang.ArithmeticException:integer overflow)。例如:Math.multiplyExact(100000000, 1000000000);
if (x > Integer.MAX_VALUE - 1) { throw new Exception("Overflow"); }
- user5063151