WHAT ARE IndexOutOfBoundsException AND HOW TO AVOID THEM?

WHAT ARE IndexOutOfBoundsException AND HOW TO AVOID THEM?

Moving along through the detailed Java Exception Handling series we’ve been working on, today we’ll be going over the IndexOutOfBoundsException. The IndexOutOfBoundsException is thrown when attempting to access an invalid index within a collection, such as an array, vector, string, and so forth. It can also be implemented within custom classes to indicate invalid access was attempted for a collection. All Java errors implement the java.lang.Throwable interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
IndexOutOfBoundsException

public class IndexOutOfBoundsException extends RuntimeException
It comes with two subclasses used very often in coding with arrays and strings.

1. ArrayIndexOutOfBoundsException

2. StringIndexOutOfBoundsException

Following two programs discuss the above two exceptions.

1. ArrayIndexOutOfBoundsException
We know exceptions are errors that creep into the execution at the runtime and if not handled properly, the execution will be terminated abruptly. The cause of exceptions is the actual user (for whom the product is delivered) and thrown when he inputs wrong values which the JRE (Java Runtime Environment) is unable interpret or execute.

One such runtime problem, a user, can do is giving wrong index number which does not exist in the array at all. If such index number is given, what output shall to be given by the system. System (known in Java as JRE; JRE is part of JVM) reacts to it as an unchecked exception with “ArrayIndexOutOfBoundsException”.

public class IndexDemo
{
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };

try
{
System.out.println(marks[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(“Wrong index number, please enter correct number. ” + e);
}
}
}
In the above code, the input for marks[] array can be taken from keyboard. For simplicity, it is taken directly as 3. The 3rd index number does not exist in the marks array; the index number are only 0, 1 and 2. The system cannot access the element and give any output; consequently throws ArrayIndexOutOfBoundsException and is caught in catch block.

2. StringIndexOutOfBoundsException
Just like ArrayIndexOutOfBoundsException exception works on array, StringIndexOutOfBoundsException works on strings. This is an unchecked exception thrown by the system when the user accesses a character that does not exist in the string (earlier it is an array). This exception is thrown by charAt() method of String class.

public class IndexDemo
{
public static void main(String args[])
{
String str = “hello”;
try
{
System.out.println(str.charAt(5));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println(“Wrong character index number, please enter correct number. ” + e);
}
}}

WHAT ARE IndexOutOfBoundsException AND HOW TO AVOID THEM?

In the above code, the charAt(int) method returns the character present at the index value passed as parameter. It is passed as 5 where the characters lie 0 to 4. This is shown by the JVM by throwing StringIndexOutOfBoundsException exception. It is handled in catch block.


Discover more from CODE t!ps

Subscribe to get the latest posts sent to your email.

Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading