Home » Core Java » What is StringIndexOutOfBoundsException?

What is StringIndexOutOfBoundsException?

StringIndexOutOfBoundsException is an unchecked exception. It occurs when you try to access a character from a String at an index greater than the length of the String.

Below is a demo program for StringIndexOutOfBoundsException:


public class StringIndexOutOfBoundsExceptionDemo {

	public static void main(String[] args) {
		String s = "ashok";
		char charAt = s.charAt(100); 
		System.out.println(charAt);
	}

}

Below is the output when you run the above program:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 100
	at java.lang.String.charAt(String.java:686)
	at StringIndexOutOfBoundsExceptionDemo.main(StringIndexOutOfBoundsExceptionDemo.java:6)

[inFeedAds1]