EXPRESSIONS
Evaluate Operands before Operation
15.6.2
class Test {
public static void main(String[] args) {
int j = 1;
try {
int i = forgetIt() / (j = 2);
} catch (Exception e) {
System.out.println(e);
System.out.println("Now j = " + j);
}
}
static int forgetIt() throws Exception {
throw new Exception("I'm outta here!");
}
}
prints:
java.lang.Exception: I m outta here!
Now j = 1
because the left hand operand
forgetIt()
of the operator
/
throws an exception
before the right hand operand and its embedded assignment of
2
to
j
occurs.
15.6.2 Evaluate Operands before Operation
Java also guarantees that every operand of an operator (except the conditional
operators
&&
,
||
, and
? :
) appears to be fully evaluated before any part of the
operation itself is performed.
If the binary operator is an integer division
/
( 15.16.2) or integer remainder
%
( 15.16.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.
So, for example, the program:
class Test {
public static void main(String[] args) {
int divisor = 0;
try {
int i = 1 / (divisor * loseBig());
} catch (Exception e) {
System.out.println(e);
}
}
307
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved