...
| Code Block | ||
|---|---|---|
| ||
public int do_operation(int a, int b) throws ArithmeticException
{
longint temp = (long)a+(long)b;
if(a>0 && b>0 && (a >Integer.MAX_VALUE - b) || a<0 && b<0 && (a < Integer.MIN_VALUE -b))
throw ArithmeticException;
else
temp = a + b;//Value within range can perform the addition
//Do stuff return
(int)temp;
}
|
Another compliant approach would be to use the BigInteger class in this example and in the examples of the other operations using a wrapper for test of the overflow:
...