Java Program to Demonstrate the Math.SubtractExact() Method

You are currently viewing Java Program to Demonstrate the Math.SubtractExact() Method

Java program to demonstrate the Math.subtractExact() method

// Java program to demonstrate the 
// Math.subtractExact() method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    int num = Integer.MIN_VALUE;

    try {
      System.out.println("Subtraction is: " + Math.subtractExact(num, 0));
      System.out.println("Subtraction is: " + Math.subtractExact(num, 1));
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}

Output

Subtraction is: -2147483648
Exception: java.lang.ArithmeticException: integer overflow

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply