public static int safeAdd(
        int op1,
        int op2
    ) {

        if (op2 == 0) {
            return op1;
        }

        long result = ((long) op1) + ((long) op2);

        if ((result < Integer.MIN_VALUE) || (result > Integer.MAX_VALUE)) {
            StringBuilder sb = new StringBuilder(32);
            sb.append("Integer overflow: (");
            sb.append(op1);
            sb.append(',');
            sb.append(op2);
            sb.append(')');
            throw new ArithmeticException(sb.toString());
        } else {
            return (int) result;
        }

    }
public static int safeAdd ( int op1 , int op2 ) { if ( op2 == 0 ) { return op1 ; } long result = ( ( long ) op1 ) + ( ( long ) op2 ) ; if ( ( result < Integer . MIN_VALUE ) || ( result > Integer . MAX_VALUE ) ) { StringBuilder sb = new StringBuilder ( 32 ) ; sb . append ( "Integer overflow: (" ) ; sb . append ( op1 ) ; sb . append ( ',' ) ; sb . append ( op2 ) ; sb . append ( ')' ) ; throw new ArithmeticException ( sb . toString ( ) ) ; } else { return ( int ) result ; } }
/*[deutsch]
<p>Addiert die Zahlen mit &Uuml;berlaufkontrolle. </p>

@param   op1     first operand
@param   op2     second operand
@return  sum
@throws  ArithmeticException if int-range overflows