code: public static int safeSubtract(int a, int b) {
          int result = a - b;
          // check for a change of sign in the result when the inputs have the different signs
          if ((a ^ result) < 0 && (a ^ b) < 0) {
              throw new ArithmeticException("Subtraction overflows an int: " + a + " - " + b);
          }
          return result;
      }
docs: Safely subtracts one int from another.
  
  @param a  the first value
  @param b  the second value to subtract from the first
  @return the result
  @throws ArithmeticException if the result overflows an int