It's pretty easy to test this and find out:
$ cat Demo1.java
class Demo1 {
public static void main(String[] args) {
try {
int[] array = {};
int impossible = 1/0 + array[0];
} catch (Exception e) {
System.out.println("Caught " + e.getClass().getSimpleName());
}
}
}
$ cat Demo2.java
class Demo2 {
public static void main(String[] args) {
try {
int[] array = {};
int impossible = array[0] + 1/0;
} catch (Exception e) {
System.out.println("Caught " + e.getClass().getSimpleName());
}
}
}
$ cat Demo3.java
class Demo3 {
public static void main(String[] args) {
try {
int[] array = {};
array[0] = 1/0 + array[0]; // assignment happens last
} catch (Exception e) {
System.out.println("Caught " + e.getClass().getSimpleName());
}
}
}
$ javac Demo*.java
$ java Demo1
Caught ArithmeticException
$ java Demo2
Caught ArrayIndexOutOfBoundsException
$ java Demo3
Caught ArithmeticException
As you can see neither has priority, instead it's order dependent. The subcomponents of an expression are evaluated in-order left to right, therefore if the left-hand operand raises an exception the right hand side is never evaluated.
Since the exception occurs during expression evaluation the assignment into array[2]
never occurs, because assignment can't happen until the expression on the right-hand-side has been executed into a value that can be assigned.
This answer goes into more detail about the relevant Java Language Specifications that define this behavior.