• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IndexOutOfBoundsException and StringIndexOutOfBoundsException

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 41905
909
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic