Hi I have written below code. After each execution it is displaying output in a different order.
public class ExcetionInFinallyBlockDemo1 {
public static void main(String[] args) {
int a[] = new int[5];
try {
System.out.println("Accessing sixth element" + a[5]);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} finally {
System.out.println("finally block start");
try {
double divide = a[3] / 0;
System.out.println("Divide result:" + divide);
} catch (ArithmeticException e2) {
e2.printStackTrace();
}
System.out.println("Finall block end");
}
System.out.println("rest of the code");
}
}
first run output:
java.lang.ArrayIndexOutOfBoundsException: 5
at com.sanju.exceptionHandling.ExcetionInFinallyBlockDemo1.main(ExcetionInFinallyBlockDemo1.java:11)
java.lang.ArithmeticException: / by zero
finally block start
at com.sanju.exceptionHandling.ExcetionInFinallyBlockDemo1.main(ExcetionInFinallyBlockDemo1.java:17)
Finall block end
rest of the code
second run output:
java.lang.ArrayIndexOutOfBoundsException: 5
at com.sanju.exceptionHandling.ExcetionInFinallyBlockDemo1.main(ExcetionInFinallyBlockDemo1.java:11)
finally block start
Finall block end
rest of the code
java.lang.ArithmeticException: / by zero
at com.sanju.exceptionHandling.ExcetionInFinallyBlockDemo1.main(ExcetionInFinallyBlockDemo1.java:17)
Third run output:
java.lang.ArrayIndexOutOfBoundsException: 5
at com.sanju.exceptionHandling.ExcetionInFinallyBlockDemo1.main(ExcetionInFinallyBlockDemo1.java:11)
java.lang.ArithmeticException: / by zero
at com.sanju.exceptionHandling.ExcetionInFinallyBlockDemo1.main(ExcetionInFinallyBlockDemo1.java:17)
finally block start
Finall block end
rest of the code
Could you please explain why it is printing in different order each time ?