harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dmitry M. Kononov (JIRA)" <j...@apache.org>
Subject [jira] Commented: (HARMONY-436) [classlib][luni] lang: Harmony should throw exceptions in a way similar to RI's one.
Date Wed, 10 May 2006 11:59:04 GMT
    [ http://issues.apache.org/jira/browse/HARMONY-436?page=comments#action_12378895 ] 

Dmitry M. Kononov commented on HARMONY-436:
-------------------------------------------

Hi George,

Thank you for pointing this out. I will explain my thoughts about these issues a little.

StringBuffer.append(char[], int, int)

The spec is not as clear in this case as it might be. You are correct it does not mention
about any exception in the description of this method. However, it reads

"The overall effect is exactly as if the arguments were converted to a string by the method
String.valueOf(char[],int,int) and the characters of that string were then appended to this
character sequence."

That is, it shows implicitly how this method should behave. The spec of the mentioned String.valueOf(char[],int,int)
method says that it throws the IndexOutOfBoundsException.
This is why I think we need to throw an exception from this method. Why this exception is
IndexOutOfBoundsException instead of ArrayIndexOutOfBoundsException please read below.

All these issues except #3 have the same reason to change StringIndexOutOfBoundsException
with IndexOutOfBoundsException.
RI throws ArrayIndexOutOfBoundsException indirectly from the System.arraycopy() method. That
is, this exception class is defined by the certain implementation that uses the System.arraycopy()
method.

Here is a stack trace which is typical for all the issues.
--
.java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.lang.AbstractStringBuilder.insert(Unknown Source)
	at java.lang.StringBuffer.insert(Unknown Source)
	at StringBufferTest.test_insert_int_char(StringBufferTest.java:12)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at junit.textui.TestRunner.doRun(TestRunner.java:116)
	at junit.textui.TestRunner.start(TestRunner.java:172)
	at junit.textui.TestRunner.main(TestRunner.java:138)
--

I thought we can not throw ArrayIndexOutOfBoundsException, since we have another implementation,
where we check the given parameters and throw an exception explicitly.

For example, let's look at the following method.

public synchronized StringBuffer insert(int index, char ch) {
	if (0 <= index && index <= count) {
		move(1, index);
		value[index] = ch;
		count++;
		return this;
	}
	throw new StringIndexOutOfBoundsException(index);
}

The spec gives us the unambiguous instruction to throw IndexOutOfBoundsException. ArrayIndexOutOfBoundsException
will look oddity here, won't it?

The next question is why we throw StringIndexOutOfBoundsException in spite of the spec directions?
I guess, the reason is to make things better than the spec does. Actually, there are a lot
of reasons, since the java documentation is often unclear and contradictory.

Anyway, I see your point of view (I see mine either. :). I agree with your objections and
I am ready to change the patch appropriately.

Do you think we should fix issue #3 only?

Thanks.

> [classlib][luni] lang: Harmony should throw exceptions in a way similar to RI's one.
> ------------------------------------------------------------------------------------
>
>          Key: HARMONY-436
>          URL: http://issues.apache.org/jira/browse/HARMONY-436
>      Project: Harmony
>         Type: Bug

>   Components: Classlib
>     Reporter: Dmitry M. Kononov
>     Assignee: George Harley
>     Priority: Minor
>  Attachments: lang.cumulative.diff
>
> 1) java.lang.StringBuilder.insert(int offset, char c):
> Harmony throws StringIndexOutOfBoundsException when offset <0,
> while RI throws ArrayIndexOutOfBoundsException. 
> Specification mentions neither StringIndexOutOfBoundsException
> nor ArrayIndexOutOfBoundsException throwing, but says: 
> "Throws:
> IndexOutOfBoundsException - if the offset is invalid".
> 2) java.lang.StringBuffer.append(char[] str, int offset, int len):
> Harmony throws StringIndexOutOfBoundsException when str is not
> null, offset or/and len <0, while RI throws
> ArrayIndexOutOfBoundsException. Specification mentions neither
> StringIndexOutOfBoundsException nor
> ArrayIndexOutOfBoundsException throwing.
> 3) java.lang.StringBuffer.append(char[] str, int offset, int len):
> Harmony throws StringIndexOutOfBoundsException when str = null,
> offset or/and len <0, while RI throws NullPointerException.
> Specification mentions neither StringIndexOutOfBoundsException
> nor NullPointerException throwing.
> 4) java.lang.StringBuffer.getChars(int srcBegin,int srcEnd,char[] dst, int dstBegin):
> Harmony throws StringIndexOutOfBoundsException when dstBegin <0,
> while RI throws ArrayIndexOutOfBoundsException.
> Specification says:
> "Throws: 
> IndexOutOfBoundsException - if any of the following is true: 
> ...
> dstBegin is negative". 
> So RI does not comply with spec as well as Harmony does not comply with it.
> 5) java.lang.StringBuffer.insert(int offset, char c):
> Harmony throws StringIndexOutOfBoundsException when offset <0,
> while RI throws ArrayIndexOutOfBoundsException.
> Specification says:
> "The offset argument must be greater than or equal to 0 ... 
> Throws: 
> IndexOutOfBoundsException - if the offset is invalid."
> So RI does not comply with spec as well as Harmony does not comply with it.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


Mime
View raw message