public static int safeMultiply ( int a , int b ) { long total = ( long ) a * ( long ) b ; if ( total < Integer . MIN_VALUE || total > Integer . MAX_VALUE ) { throw new ArithmeticException ( "Multiplication overflows an int: " + a + " * " + b ) ; } return ( int ) total ; }
Safely multiply one int by another .