StringIndexOutOfBoundsException in java

IndexOutOfBoundsException in Java


IndexOutOfBoundsException class is available in java.lang package and it extends RuntimeException class. It is an unchecked exception.


public class IndexOutOfBoundsException extends RuntimeException


IndexOutOfBoundsException has two sub classes i.e, ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException.

It occurs whenever access the unknown index that is negative or greater than the size of an array or string.

Hierarchy of IndexOutOfBoundsException




StringIndexOutOfBoundsException in java


StringIndexOutOfBoundsException class is available in java.lang package and it extends IndexOutOfBoundsException class. It is an unchecked exception.


public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException


StringIndexOutOfBoundsException class inherits methods from java.lang.Object class and java.lang.Throwable class.

When StringIndexOutOfBoundsException Occurs


StringIndexOutOfBoundsException occurs whenever any of string function try to access the String using an index and if that index value is out of the string length.

For example, when we access a character from the string which doesn't exist.
String name= “world”;, length of the string is 5(0th to 4th index)

This is the actually string index position
w
o
r
l
d
        0            1                2            3             4

Example:



package com.exceptions;
public class SIOBExceptionTest {
       public static void main(String[] args) {
              String name="world";
              System.out.println(name.charAt(5));//StringIndexOutOfBoundsException
       }
}


Output:



Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
       at java.lang.String.charAt(Unknown Source)
       at com.exceptions.SIOBExceptionTest.main(SIOBExceptionTest.java:5)


This is the one case to occure StringIndexOutOfBoundsException when we accessed out of the string size index i.e, 5th index.

If we notice in the output charAt(Unknown Source) that mean we tried to access unknown index.

w
o
r
l
d
Greater than the size of the string
        0             1                 2              3              4                                      5

Handling StringIndexOutOfBoundsException using try-catch block



package com.exceptions;
public class SIOBExceptionTest {
       public static void main(String[] args) {
              String name="world";
              try {
                     System.out.println(name.charAt(5));
              } catch (StringIndexOutOfBoundsException e) {
                     System.out.println("accessing unknown index, provied correct index "+e);
              }
       }
}


Output:



accessing unknown index, provied correct index java.lang.StringIndexOutOfBoundsException: String index out of range: 5


In the try block we placed code which is throw an StringIndexOutOfBoundsException by JVM (that we are trying to accessing unknown index i.e, 5th index which is doesn’t existed). It is handled in catch block.




No comments:

Post a Comment