...
Code Block | ||
---|---|---|
| ||
if(a == Integer.MIN_VALUE && b == -1) throw ArithmeticException;//May be Integer.MIN_VALUE again???? else result = a/b;//safe operation |
Modulo
Modulo operation is safer in Java than C/C++
- For modulo operator the only problem is if we take the modulo of Integer.MIN_VALUE with -1 . The the result is always 0 in JAVA so we are back to the previous rule (for division)
-if the right-hand operand is zero the integer remainder operator %, which throw an ArithmeticException
Unary Negation
If we negate Integer.MIN_VALUE we get Integer.MIN_VALUE. So we explicitely check the range
...