code: public static int safeAdd(int a, int b) {
          int sum = a + b;
          // check for a change of sign in the result when the inputs have the same sign
          if ((a ^ sum) < 0 && (a ^ b) >= 0) {
              throw new ArithmeticException("Addition overflows an int: " + a + " + " + b);
          }
          return sum;
      }
docs: Safely adds two int values.
  
  @param a  the first value
  @param b  the second value
  @return the result
  @throws ArithmeticException if the result overflows an int