How to handle ArrayIndexOutOfBoundsException :JAVA ,ANDROID

Jyotishgher Astrology
By -
0
How to handle ArrayIndexOutOfBoundsException :JAVA ,ANDROID


java.lang.ArrayIndexOutOfBoundsException

java.lang.ArrayIndexOutOfBoundsException occurs when we try to access an element of an array, with an index that is negative or more than the size of array itself. Usually, one would come across “java.lang.ArrayIndexOutOfBoundsException: 4” which occurs when an attempt is made to access (4+1)fifth element of an array, despite the size of array being less than five.
Lets see a sample program that could produce this exception:
Let us see “java.lang.ArrayIndexOutOfBoundsException: 4” in detail:
  • java.lang.ArrayIndexOutOfBoundsException : Exception thrown by java language while trying to access an element of array with an index that is out of the range of
  • 4 : is the index of element, that we tried to access in the array. This goes in, while creating the exception object.

How to handle ArrayIndexOutOfBoundsException ?

Let’s handle the ArrayIndexOutOfBoundsException using try-catch.
  • Surround the statements that could throw ArrayIndexOutOfBoundsException with try-catch block.
  • Catch ArrayIndexOutOfBoundsException.
  • Take necessary action for the further course of your program, as you are handling the exception and the execution doesn’t abort.

Post a Comment

0Comments

Post a Comment (0)