In this Java tutorial, you will learn when java.lang.StringIndexOutOfBoundsException occurs, and how to handle this exception.

How does StringIndexOutOfBoundsException occur ?

String is kind of an ensemble of characters. String object has a range of [0,length of the string]. When someone tries to access the characters with limits exceeding the range of actual string value, java.lang.StringIndexOutOfBoundsException: String index out of range occurs.

Example

In the following program, we shall try to access the characters out of range, using String.substring() method.

Example.java

</>
Copy
public class Example {
	public static void main(String[] args) {
		String str = "Learning Java is easy.";
		String strPart = str.substring(14,36);
		System.out.println("strPart : " +strPart);
	}
}

When the program is run, str.substring(14,36) tries to access characters from index 14 to 36 in the string str. Since the length of the string is only 21, java runtime does not allow us to access any character beyond the limit of 21, which in this case thows java.lang.StringIndexOutOfBoundsException: String index out of range: 36 as shown below.

strPart1 : Learning Java
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 36
	at java.lang.String.substring(Unknown Source)
	at com.tut.StringIndexOutOfBoundsExceptionExample.main(StringIndexOutOfBoundsExceptionExample.java:12)

How to handle java.lang.StringIndexOutOfBoundsException: String index out of range ?

1. Manually check the string length

To handle this exception by yourself, check the string length before operations like substring.

</>
Copy
public class Example {
	public static void main(String[] args) {
		String str = "Learning Java is easy.";
		System.out.println("Length of str is : "+str.length());
		if(13<str.length()){
			String strPart1 = str.substring(0,13);
			System.out.println("strPart1 : " +strPart1);
		}
		if(36<=str.length()){
			String strPart2 = str.substring(14,36);
			System.out.println("strPart2 : " +strPart2);
		}
		System.out.println("Action of accessing chars is taken after checking range of string, str. StringIndexOutOfBoundsException is handled.");
	}
}

When the program is run, as the str.length = 22 and 36 is not less than or equal to 22, we avoided executing the line that shall cause StringIndexOutOfBoundsException.

Output

Length of str is : 22
strPart1 : Learning Java
Action of accessing chars is taken after checking range of string, str. StringIndexOutOfBoundsException is handled.

2. Use try-catch

You can also use try-catch block around the code snippet, that could possibly throw StringIndexOutOfBoundsException, as shown in the following program.

public class Example {
	public static void main(String[] args) {
		String str = "Learning Java is easy.";
		try {
			String substr = str.substring(20,36);
			System.out.println("Sub string : " +substr);
		} catch (StringIndexOutOfBoundsException e) {
			System.out.println("Exception caught in catch block.");
		}
	}
}

When the program is run, str.substring(14,36) causes StringIndexOutOfBoundsException, where in the catch block has cought the exception and proceed with the rest of execution. The output is shown below:

Exception caught in catch block.

Java Reference

Java doc to java.lang StringIndexOutOfBoundsException is available at https://docs.oracle.com/javase/8/docs/api/java/lang/StringIndexOutOfBoundsException.html

Conclusion

In this tutorial, we have presented what java.lang StringIndexOutOfBoundsException: String index out of range exception is, and how to handle this exception in two different ways.