Java : StringIndexOutOfBoundsException with Examples
StringIndexOutOfBoundsException (Java SE 21 & JDK 21) with Examples.
You will find code examples on most StringIndexOutOfBoundsException methods.
Summary
final var str = "abc";
System.out.println(str.charAt(0)); // a
System.out.println(str.charAt(1)); // b
System.out.println(str.charAt(2)); // c
try {
final var ret = str.charAt(3);
} catch (StringIndexOutOfBoundsException e) {
System.out.println(e);
}
// Result
// ↓
//java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
Constructors
StringIndexOutOfBoundsException ()
final var e = new StringIndexOutOfBoundsException();
System.out.println(e); // java.lang.StringIndexOutOfBoundsException
StringIndexOutOfBoundsException (int index)
final var e = new StringIndexOutOfBoundsException(123);
// java.lang.StringIndexOutOfBoundsException: String index out of range: 123
System.out.println(e);
StringIndexOutOfBoundsException (String s)
final var e = new StringIndexOutOfBoundsException("abcd");
System.out.println(e); // java.lang.StringIndexOutOfBoundsException: abcd
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Related posts
- API Examples
- Error
- Exception
- RuntimeException
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- CancellationException
- ClassCastException
- ConcurrentModificationException
- DateTimeException
- DateTimeParseException
- IllegalArgumentException
- IllegalStateException
- IndexOutOfBoundsException
- NoSuchElementException
- NullPointerException
- PatternSyntaxException
- UnsupportedOperationException
- Throwable