Java Reference
In-Depth Information
the array access expression have been evaluated and only if these evaluations
completed normally. Thus, the program:
Click here to view code image
class Test3 {
public static void main(String[] args) {
int index = 1;
try {
nada()[index=2]++;
} catch (Exception e) {
System.out.println(e + ", index=" + index);
}
}
static int[] nada() { return null; }
}
prints:
java.lang.NullPointerException, index=2
because the embedded assignment of 2 to index occurs before the check for a
null array reference expression. As a related example, the program:
Click here to view code image
class Test4 {
public static void main(String[] args) {
int[] a = null;
try {
int i = a[vamoose()];
System.out.println(i);
} catch (Exception e) {
System.out.println(e);
}
}
static int vamoose() throws Exception {
throw new Exception("Twenty-three skidoo!");
}
}
always prints:
java.lang.Exception: Twenty-three skidoo!
Search WWH ::




Custom Search