Why doesn't the code generate ArrayIndexOutOfBoundsException? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Why doesn't the code generate ArrayIndexOutOfBoundsException?

The code should produce two errors: 1.ArrayIndexOutOfBoundsException when I tried to access the 8th index of an array of size 4. 2. ArithmeticException when I divided by 0. It only throws the Arithmetic one. https://code.sololearn.com/c4RGf6V66C9H/?ref=app

30th Nov 2018, 2:15 AM
Gammaburst
Gammaburst - avatar
5 Answers
+ 7
array[8] = 40/0; In programming, the values to the right of the equals sign is evaluated first before it is assigned to a variable. Therefore, the Arithmetic exception and finally blocks are executed instead of the ArrayIndexOutOfBoundsException. To prove my point check the following: https://code.sololearn.com/cl8RDsrSB3MW/?ref=app
30th Nov 2018, 4:35 AM
Lambda_Driver
Lambda_Driver - avatar
+ 5
No problem. We are all here to learn. I'm just a noob compared to the others here but I love learning from my own mistakes. 😆
30th Nov 2018, 4:49 AM
Lambda_Driver
Lambda_Driver - avatar
+ 1
In java if you declare int arr[10] and trying to point on arr[10]=50 or anything else this exception is generated... Because it is invalid location...
30th Nov 2018, 12:25 PM
Pawan Bangar
Pawan Bangar - avatar
+ 1
Whenever We try to assign Variable a value the Variable which we are trying to be assigned a value is placed on the left side of assignment operator....and the value is placed on the right........if it is an expression it would be evaluated first and then the resulting value would be assigned to the variable.....That’s why it is throwing an ArithmaticException
1st Dec 2018, 5:19 PM
Subrata Karmakar
Subrata Karmakar - avatar
0
Thanks Lambda_Driver for taking the time to explain it to me. 😄
30th Nov 2018, 4:37 AM
Gammaburst
Gammaburst - avatar