Java Reference
In-Depth Information
If the binary operator is an integer division / 15.17.2 ) or integer remainder % 15.17.3 ) ,
then its execution may raise an ArithmeticException , but this exception is thrown only after
both operands of the binary operator have been evaluated and only if these evaluations
completed normally.
Example 15.7.2-1. Evaluation of Operands Before Operation
Click here to view code image
class Test {
public static void main(String[] args) {
int divisor = 0;
try {
int i = 1 / (divisor * loseBig());
} catch (Exception e) {
System.out.println(e);
}
}
static int loseBig() throws Exception {
throw new Exception("Shuffle off to Buffalo!");
}
}
This program produces the output:
java.lang.Exception: Shuffle off to Buffalo!
and not:
java.lang.ArithmeticException: / by zero
since no part of the division operation, including signaling of a divide-by-zero excep-
tion, may appear to occur before the invocation of loseBig completes, even though the
implementation may be able to detect or infer that the division operation would cer-
tainly result in a divide-by-zero exception.
15.7.3. Evaluation Respects Parentheses and Precedence
The Java programming language respects the order of evaluation indicated explicitly by
parentheses and implicitly by operator precedence.
An implementation of the Java programming language may not take advantage of al-
gebraic identities such as the associative law to rewrite expressions into a more con-
venient computational order unless it can be proven that the replacement expression
is equivalent in value and in its observable side effects, even in the presence of mul-
Search WWH ::




Custom Search