-4

The following code gives the desired output which is "Arithmetic Exception occurs"

public static void main(String[] args) {

    try {
        int []a = new int[5];
        a[5] = 30/0; 
    } catch(ArithmeticException e) {
        System.out.println("Arithmetic Exception occurs");
    } catch(ArrayIndexOutOfBoundsException e) {
        System.out.println("ArrayIndexOutOfBounds Exception occurs");
    } catch(Exception e) {
        System.out.println("Parent Exception occurs");
    }
    System.out.println("rest of the code");
}

but if I want to get two exceptions at a time, the modified code will be

public static void main(String[] args) {

    try {
        int []a = new int[5];
        a[10] = 30/0; 
    } catch(ArithmeticException e) {
        System.out.println("Arithmetic Exception occurs");
    } catch(ArrayIndexOutOfBoundsException e) {
        System.out.println("ArrayIndexOutOfBounds Exception occurs");
    } catch(Exception e) {
        System.out.println("Parent Exception occurs");
    }
    System.out.println("rest of the code");
}

but instead of giving both, "Arithmetic Exception occurs" & "ArrayIndexOutOfBounds Exception occurs" the output is only "Arithmetic Exception occurs"

Can anyone explain this?

Matt
  • 12,848
  • 2
  • 31
  • 53
  • 4
    Simple: once the first exception is thrown, program control does not reach the array element assignment. – M A Jun 02 '21 at 06:28
  • You realize that the first exception encountered is being thrown and there will only be up to one catch block handling it, don't you? What would be the point of continuing normally after the first exception anyway? – Thomas Jun 02 '21 at 06:28
  • The first error that is encountered is going to break out of the "try" block, and then go to the appropriate "catch" block. Thus, in this instance, your a[10] ... line never gets reached, so won't trigger a "catch" – Craig Jun 02 '21 at 06:29
  • When you use a debugger, you can observe for yourself what @Craig already mentioned. The exception gets thrown before the array is accessed. The exception is then catched by the appropriate catch-block and execution continues there. – Matt Jun 02 '21 at 06:32
  • To make the problem obvious to see, you can split the line into two lines: `int temp = 30/0; a[10] = temp;` Now it should be clear that the second line is never accessed because the first line throws an exception. – Matt Jun 02 '21 at 06:36
  • thank you everyone for your response. So it's like if one exception is gets thrown then the code doesn't go for further... Am I right?? – Aditya Arpan Sahoo Jun 02 '21 at 06:47
  • runtime behavior is not same as compiler - which can show multiple errors – HPKG Jun 02 '21 at 06:50
  • 1
    *"Am I right?"* - Yes. Which means that your goal of having both exceptions thrown is unattainable. The best you could do is throw one exception, catch it, then throw the second one. And even then, they are not both "in play" at the same time. – Stephen C Jun 02 '21 at 06:56
  • 1
    Think about it. If `30/0` fails and doesn’t produce a value, what value should `a[10] = …` attempt to store in the array? If it can’t attempt to store a value in `a[10]` it can’t fail with an `ArrayIndexOutOfBoundsException`. – Holger Jun 07 '21 at 15:38

1 Answers1

0

For this line

a[10] = 30/0; 

As soon as 30/0 is evaluated, exception is thrown.

At any point, the code can throw single exception. Even if you catch on the exception and its base class, you can run single catch statement.

HPKG
  • 335
  • 1
  • 9