是否有可能在发生整数溢出时抛出某种运行时异常,而不是静默失败.例如
int x = 100000000 * 1000000000;
Run Code Online (Sandbox Code Playgroud)
1569325056
由于溢出而打印,我想要的是获得某种运行时异常
sol*_*4me 14
是的,从Java-8开始,您可以使用新的Exact方法,它会在溢出时抛出异常(java.lang.ArithmeticException:integer overflow).例如
Math.multiplyExact(100000000, 1000000000);
Run Code Online (Sandbox Code Playgroud)