Javatpoint Logo
Javatpoint Logo

StringIndexOutOfBoundsException in Java

In Java, a StringIndexOutOfBoundsException is a runtime exception when you try to access a character in a String at an invalid index.

Attempting to access a character at an index that is either negative or outside the range of the String's length causes this exception to be thrown.

For example, if you have a String variable called myString with a value of "Hello", trying to access myString.charAt(5) will result in a StringIndexOutOfBoundsException because the index 5 is out of bounds (valid indices for this String would be 0 through 4).

You can handle this exception using a try-catch block. The catch block can then handle the exception and provide an appropriate error message to the user.

Causes StringIndexOutOfBoundsException

In Java, several methods can throw a StringIndexOutOfBoundsException when invalid specified arguments are used. Here are some examples:

  1. charAt(int index) method of the String class: The specified index in the String is used to return the character by this method. A StringIndexOutOfBoundsException is thrown if the index is less than 0 or more than or equal to the length of the String.
  2. substring(int beginIndex, int endIndex) method of the String class: The method returns a new String that is a substring of the original String. A StringIndexOutOfBoundsException is thrown if either the beginIndex or endIndex parameter is less than 0 or more than the length of the String, or if beginIndex is larger than endIndex.
  3. substring(int beginIndex): In Java, the String.substring(int beginIndex) method returns a new String that is a substring of the original String, beginning at the specified beginIndex and extending to the end of the original String. The beginIndex is inclusive, meaning that the character at the beginIndex is included in the returned String.
  4. valueOf(char[] data, int offset, int count): Java's method creates a new string by extracting a substring from the specified character array data. The substring starts at the index specified by offset and is of length count. If the offset and count values are out of bounds, a StringIndexOutOfBoundsException is thrown.
  5. subSequence(int beginIndex, int endIndex): It is a method in Java that returns a new CharSequence that is a subsequence of the original CharSequence object. The subsequence begins at the beginIndex and continues until the character at position endIndex -1. The returned CharSequence shares the same underlying character sequence as the original object.

StringIndexOutOfBoundsException Example



Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 2, end 8, length 7
	at java.base/java.lang.String.checkBoundsBeginEnd(
at java.base/java.lang.String.substring(
	at StringIndexOutOfBoundsExceptionExample.main(

Explanation: In this example, the substring() method is called on a String with a specified beginIndex of 2 and endIndex of 8, which is out of bounds since the String only has a length of 7 (indices range from 0 to 6). As a result, the substring() method throws a StringIndexOutOfBoundsException.

Handling StringIndexOutOfBoundsException

As the StringIndexOutOfBoundsException is a Java exception, the following steps may be utilized to manage it with try-catch blocks:

  • Put try-catch blocks around any statements that might result in a StringIndexOutOfBoundsException.
  • Catch the StringIndexOutOfBoundsException.



String index is out of bounds: begin 2, end 8, length 7

Explanation: In this example, the substring() method is called on a String with specified indices of 2 and 8, which are out of bounds. The code within the try block is executed, and when the StringIndexOutOfBoundsException is thrown, it is caught by the catch block. Within the catch block, an error message is logged to the console.

Depending on the application's requirements, you could take different actions within the catch block, such as setting a default value for the substring or prompting the user to enter valid indices.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA