String.getChars behaviour differs from Sun implementation
Erwin Bolwidt
ejb@klomp.org
Thu May 24 09:40:00 GMT 2001
Dear list,
I'm using the gcc-3_0-branch of today. When I run this piece of code:
public class Test
{
public static void main(String[] args)
{
char[] array = new char[0];
"foo".getChars(0, 3, array, 0);
}
}
I get different results depending on the version and environment.
Sun JDK1.3: ArrayIndexOutOfBoundsException
gcj 2.95.4: ArrayIndexOutOfBoundsException
gcj 3.0: StringIndexOutOfBoundsException
Sun's official Javadoc isn't clear enough, it merely states that an
IndexOutOfBoundsException must be thrown in some well-defined cases, and
this true for all three tested environments.
But it seems logical to me that you get a StringIndexOutOfBoundsException
if the first two parameters of the getChars method are wrong (they point
to the start and end character within the string) and an
ArrayIndexOutOfBoundsException if the array is too small or the last
argument, the start index in the array, is negative or too big.
I ran into this problem in my own java app, and the workaround is simple:
stick to what's specified in the javadoc and only catch
IndexOutOfBoundsException. But maybe gcj should be changed because
ArrayIndexOutOfBoundsException is more logical in this case.
Best regards,
Erwin Bolwidt
P.s. I did search the bug database and mail archives first, but didn't
find any comments on this.
More information about the Java
mailing list