• 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

IndexOutOfBounds and StringIndexOutOfBounds

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what API documentations about String.subString method


substring
public String substring(int beginIndex,
int endIndex)Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
Returns:
the specified substring.

Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.



Here again API documentation about StringIndexOutOfBoundsException


public class StringIndexOutOfBoundsException
extends IndexOutOfBoundsException
Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.


My question is Can there be any confusion when dealing with questions on Strings in exam?
When I execute a simple subString() with negative index for the end index I am getting StringIndexOutOfBounds instead of IndexOutOfBounds as specified by API.
[ March 03, 2003: Message edited by: Sarma Lolla ]
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This can be confusing. Keep in mind that StringIndexOutOfBoundsException extends IndexOutOfBoundsException. This means that StringIndexOutOfBoundsException is an IndexOutOfBoundsException.
So, to more directly answer your question, it helps to understand in the inheritance hierarchy.
For example:

would work if the code that generated the exception threw either StringIndexOutOfBoundsException or an IndexOutOfBoundsException. However, the following code:

would only catch a StringIndexOutOfBoundsException.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic