...
Code Block | ||
---|---|---|
| ||
public int do_operation(int a, int b) throws ArithmeticException {
long temp = (long)a + (long)b;
if(temp > Integer.MAX_VALUE || temp < Integer.MIN_VALUE) {
throw new ArithmeticException("Not in range");
}
return (int)temp; // Value within range; can perform the addition
}
|
...
Code Block | ||
---|---|---|
| ||
public int do_operation(int a, int b) throws ArithmeticException {
if( b>0 ? a > Integer.MAX_VALUE - b : a < Integer.MIN_VALUE - b ) {
throw new ArithmeticException("Not in range");
}
return a + b; // Value within range so addition can be performed
}
|
...
Code Block | ||
---|---|---|
| ||
public int do_operation(int a, int b) throws ArithmeticException { if(b > 0 ? a < Integer.MIN_VALUE + b : a > Integer.MAX_VALUE + b ) { throw new ArithmeticException("Not in range"); } return a - b; // Value within range; can perform the addition } |
...