Java Math addExact() Method

Facebooktwitterredditpinterestlinkedinmailby feather

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int.

Syntax

public static int addExact (int x, int y)

public static long addExact (long x, long y)

Parameters

x= first value

y= second value

 Return Value

The addExact () method returns the sum of the arguments.

Throws

The addExact() method throws:

ArithmeticException- if the result overflows an int or a long

Example 1:

Output:

Example 2:

 Output:

Example 3:

 Output:

Example 4:

Output:

Facebooktwitterredditpinterestlinkedinmailby feather