-1

I wrote a program little confusing on that I know it will throw ArithmeticException but before that it will through ArrayIndexOutOfBoundsException as I expected but it always throwing ArithmeticException.

I have the below code :

try{
            int arrray[]={1,3};
            arrray[2]=3/0+arrray[5];
        }catch(ArrayIndexOutOfBoundsException ie){
            ie.printStackTrace();
        }
        catch(ArithmeticException ie){
            ie.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }

Thanks for your help

B_PRIEUR
  • 160
  • 1
  • 1
  • 18
kunu
  • 1
  • 4
  • 1
    Depends on your language's operator precedence. Tag your question with appropriate language. – Mat J Oct 26 '16 at 12:37
  • It has nothing to do with priority of the exceptions. There is no such thing as priority between exceptions. What there is is order of evaluation, and the division will be evaluated first, by the rules of Java, which will throw `ArithmeticException: division by zero`. – user207421 Oct 28 '16 at 01:37

3 Answers3

1

its not the priority of Exception, but in which order your expression arrray[2]=3/0+arrray[5]; is evaluated, here as per BODMAS rule division that 3/0 would be evaluated first so ArithmeticException would be called.

0

Definitely it will be ArithmeticException. According to BODMAS rule first division operator will be evaluated Divide by ZERO which will throw ArithmeticException.

dimo414
  • 47,227
  • 18
  • 148
  • 244
Bharat Jain
  • 177
  • 1
  • 16
  • if i am adding a bracket also i am getting the same exception why... i have the below code arrray[2]=3/0+(arrray[5]+1); – kunu Oct 26 '16 at 13:22
  • [Subexpressions are not evaluated in order-of-operations order](http://stackoverflow.com/q/6800590/113632). They are generally evaluated left-to-right. – dimo414 Oct 26 '16 at 15:29
  • This answer is correct. Whoever downvoted it needs to go back to grade 3. – user207421 Oct 28 '16 at 01:35
  • @dimo414 Operands in Java are *always* evaluated left to right. The JLS says so. – user207421 Oct 28 '16 at 01:37
  • @EJP fair enough, s/generally/always/. That's what I said in my answer. This answer is saying division will be evaluated first due to order-of-operations, *not* that they're evaluated left-to-right. – dimo414 Oct 28 '16 at 02:55
0

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.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244