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 example, StringIndexOutOfBoundsExceptionExample.java, we shall try to access the characters out of range, using subString method.

package com.tut;

/**
 * @author arjun
 */
public class StringIndexOutOfBoundsExceptionExample {

	public static void main(String[] args) {
		String str = "Learning Java is easy.";
		String strPart1 = str.substring(0,13);
		System.out.println("strPart1 : " +strPart1);
		String strPart2 = str.substring(14,36);
		System.out.println("strPart2 : " +strPart2);
	}
}

When the program is run, the method tries to access characters from position 14 to 36. 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 ?

Do check the range of the string, using String.length() method, and proceed accessing the characters of it accordingly.

package com.tut;

/**
 * @author arjun
 */
public class StringIndexOutOfBoundsExceptionExample {

	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. The output is as shown below:

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

Use try-catch block around the code snippet, that could possibly throw StringIndexOutOfBoundsException.

package com.tut;

/**
 * @author arjun
 */
public class StringIndexOutOfBoundsExceptionExample {

	public static void main(String[] args) {
		String str = "Learning Java is easy.";
			try {
				String strPart1 = str.substring(0,13);
				System.out.println("strPart1 : " +strPart1);
			} catch (StringIndexOutOfBoundsException e) {
				System.out.println("Tried to access str at its out of limits, for strPart1. StringIndexOutOfBoundsException is handled. ");
			}
			try {
				String strPart2 = str.substring(14,36);
				System.out.println("strPart2 : " +strPart2);
				System.out.println(strPart2);
			} catch (StringIndexOutOfBoundsException e) {
				System.out.println("Tried to access str at its out of limits, for strPart2. StringIndexOutOfBoundsException is handled. ");
			}
		System.out.println("StringIndexOutOfBoundsException is handled using try-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:

strPart1 : Learning Java
Tried to access str at its out of limits, for strPart2. StringIndexOutOfBoundsException is handled. 
StringIndexOutOfBoundsException is handled using try-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

This is what and how we handle java.lang StringIndexOutOfBoundsException: String index out of range.