Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IndexOutOfBoundsException and StringIndexOutOfBoundsException

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By api no one method from class String throws StringIndexOutOfBoundsException, all mehtod that try access irrelevant position throw IndexOutOfBoundsException.
but in StringBuilder by api next method throws IndexOutOfBoundsException:

public StringBuilder append(CharSequence s, int start, int end)
public StringBuilder append(char[] str, int offset, int len)
public StringBuilder insert(int dstOffset, CharSequence s)
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end)
public StringBuilder insert(int offset, char c)
public void setLength(int newLength)
public char charAt(int index)
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
public void setCharAt(int index, char ch)
public CharSequence subSequence(int start, int end)

public int codePointAt(int index)
public int codePointBefore(int index)
public int codePointCount(int beginIndex, int endIndex)
public int offsetByCodePoints(int index, int codePointOffset)

and other throws StringIndexOutOfBoundsException.

Is the simple rule to remember witch from method StringBuilder must throw IndexOutOfBoundsException?
On exam is question of type exception that throw method from class String and class StringBuilder?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to known an exception is thrown when accessing a bad index (of a string, string builder, array or arraylist). You don't need to know which of those exception types in particular.

Also note that none of the codePoint methods are in scope for the exam.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to add 2 remarks.

1/ StringIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException

2/ Although the javadoc of class String mentions an IndexOutOfBoundsException is thrown, these methods actually throw a StringIndexOutOfBoundsException. As illustrated by the following code snippets and their stack traces.

Stack trace: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at String1.main(Snippets.java:217)


Stack trace: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(String.java:1907)
at String2.main(Snippets.java:222)


Just my 2 cents!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic