Math.incrementExact()
Java Math.incrementExact() method
Last Updated : 17 Mar 2025
The java.lang.Math.incrementExact() returns the argument incremented by one. It will throw an exception if the result overflows either int or long.
Syntax
Parameter
Return
- If the argument is Integer.MAX_VALUE or Long.MAX_VALUE, it will throw an ArithmeticException.
Example 1
Output:
Example 2
Output:
Example 3
Output:
Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.lang.Math.incrementExact(Math.java:909) at incrementExactExample3.main(incrementExactExample3.java:7)
Example 4
Output:
Exception in thread "main" java.lang.ArithmeticException: long overflow at java.lang.Math.incrementExact(Math.java:926) at incrementExactExample4.main(incrementExactExample4.java:7)