IndexOutOfBoundsException

IndexOutOfBoundsException

IndexOutOfBoundsException

Summary: In this tutorial, you learn handling of IndexOutOfBoundsException with its subclasses.

IndexOutOfBoundsException is an unchecked exception that extends RuntimeException. This is used by the JVM to check the index number entered by the user is within the range of an array or string.

IndexOutOfBoundsException

Full hierarchy of exceptions is available at "Hierarchy of Exceptions – Checked/Unchecked Exceptions".

Following is the class signature

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".

Following is the class signature

public class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException

Following program explains.

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.

This program gives emphasis on the cause of ArrayIndexOutOfBoundsException. The style and syntax of exception handling (try-catch) is available at "Exception Handling : try – catch".

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.

Following is the class signature

public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException

Following program explains.

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.

1. Do you know, a try block can be written with finally block without catch block?
2. Do you know, the three styles of handling exceptions.
3. Do you know, the rules imposed by exceptions in method overriding?

Leave a Reply

Your email address will not be published. Required fields are marked *