kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Length: 13
0th Character: H
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 14
kodingwindow@kw:~$
Java program to handle the StringIndexOutOfBoundsException
KW.java
classKW{publicstaticvoidmain(Stringargs[]){Strings="Hello, World!";System.out.println("Length: "+s.length());System.out.println("0th Character: "+s.charAt(0));try{System.out.println("14th Character: "+s.charAt(14));}catch(StringIndexOutOfBoundsExceptione){System.out.println("Requested character not found");}}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Length: 13
0th Character: H
Requested character not found
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.