Name: nt126004 Date: 10/16/2001
java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)
Note that this is also a problem in 1.3 and probably earlier.
Short test program:
public class Test
{
public static void main (String [] args)
throws Exception
{
String s = "Hello";
s.substring (4, 1);
}
}
I would expect this to give an IndexOutOfBoundsException, which it does.
Unfortunately, the message is far from helpful:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -3
at java.lang.String.substring(String.java:1443)
at Test.main(Test.java:8)
The index -3 is never used, and I can't easily think of any substring
algorithm which *would* end up using it.
I would suggest that IllegalArgumentException should be used rather than an
IndexOutOfBoundsException, as that's more descriptive. Failing that, a better
error message should be used.
(Review ID: 133804)
======================================================================
Name: rmT116609 Date: 08/12/2004
A DESCRIPTION OF THE REQUEST :
Currently if the method substring(start, end) is called on an instance of String, and start is greater than end, then the exception thrown by the method is a StringIndexOutOfBoundsException with an out of bounds value of (end - start).
For debugging issues this is confusing, for it semantically would imply that either of the end points is out of bounds, not that there is something wrong with the range definition.
It would be more informative if the exception thrown implied there was a problem with the range -- at least something informative in the message string of the exception.
JUSTIFICATION :
More informative exceptions == good for debugging.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Start index of 5 is greater than end index of 3
at java.lang.String.substring(Unknown Source)
at StringSubstringBlah.main(StringSubstringBlah.java:5)
ACTUAL -
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(Unknown Source)
at StringSubstringBlah.main(StringSubstringBlah.java:5)
---------- BEGIN SOURCE ----------
public class StringSubstringBlah {
static public void main (String[] args) {
String str = "This is a string";
String substr = str.substring(5, 3);
}
}
---------- END SOURCE ----------
(Review ID: 296465)
======================================================================
java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)
Note that this is also a problem in 1.3 and probably earlier.
Short test program:
public class Test
{
public static void main (String [] args)
throws Exception
{
String s = "Hello";
s.substring (4, 1);
}
}
I would expect this to give an IndexOutOfBoundsException, which it does.
Unfortunately, the message is far from helpful:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -3
at java.lang.String.substring(String.java:1443)
at Test.main(Test.java:8)
The index -3 is never used, and I can't easily think of any substring
algorithm which *would* end up using it.
I would suggest that IllegalArgumentException should be used rather than an
IndexOutOfBoundsException, as that's more descriptive. Failing that, a better
error message should be used.
(Review ID: 133804)
======================================================================
Name: rmT116609 Date: 08/12/2004
A DESCRIPTION OF THE REQUEST :
Currently if the method substring(start, end) is called on an instance of String, and start is greater than end, then the exception thrown by the method is a StringIndexOutOfBoundsException with an out of bounds value of (end - start).
For debugging issues this is confusing, for it semantically would imply that either of the end points is out of bounds, not that there is something wrong with the range definition.
It would be more informative if the exception thrown implied there was a problem with the range -- at least something informative in the message string of the exception.
JUSTIFICATION :
More informative exceptions == good for debugging.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Start index of 5 is greater than end index of 3
at java.lang.String.substring(Unknown Source)
at StringSubstringBlah.main(StringSubstringBlah.java:5)
ACTUAL -
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(Unknown Source)
at StringSubstringBlah.main(StringSubstringBlah.java:5)
---------- BEGIN SOURCE ----------
public class StringSubstringBlah {
static public void main (String[] args) {
String str = "This is a string";
String substr = str.substring(5, 3);
}
}
---------- END SOURCE ----------
(Review ID: 296465)
======================================================================