Is it possible to throw some sort of runtime exception when integer overflow occurs rather then failing silently. For e.g.
int x = 100000000 * 1000000000;
print 1569325056
due to overflow and what I w'd like is to get some sort of runtime exception
Yes, Starting from Java-8 you can use the new Exact method, it will throw an exception(java.lang.ArithmeticException: integer overflow) on overflow. E.g.
Math.multiplyExact(100000000, 1000000000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With