Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long.

Syntax:

  1. public static int subtractExact (int x, int y)
  2. public static long subtractExact (long x, long y)

Parameters:

The parameters ‘x’ and ‘y’ represent the first and second number.

Return Value:

The subtractExact () method returns the result after subtraction of the specified arguments.

Throws:

The subtractExact () method throw:

ArithmeticException- if the result overflows int or long.

 Example 1:

Output:

Example 2:

Output:

Example 3:

Output: