Reputation: 10872
I'm reading the JavaDoc of System.arraycopy
and found something confused me.
... if any of the following is true, an
IndexOutOfBoundsException
is thrown and the destination is not modified:
- The
srcPos
argument is negative.- The
destPos
argument is negative.- The
length
argument is negative.srcPos+length
is greater thansrc.length
, the length of the source array.destPos+length
is greater thandest.length
, the length of the destination array.
And in the statement of throws
, it says
IndexOutOfBoundsException - if copying would cause access of data outside array bounds.
I'm wondering...
Why they use IndexOutOfBoundsException but not ArrayIndexOutOfBoundsException?
I tried all the circumstances stated above, and all threw ArrayIndexOutOfBoundsException actually.
So is there any reason so that they avoid using the subclass of IndexOutOfBoundsException in the JavaDoc?
Upvotes: 1
Views: 1590
Reputation: 1468
From the javadoc:
ArrayIndexOutOfBoundsException
:
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
IndexOutOfBoundsException
:
Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.
The IndexOutOfBoundsException
is thrown when any index is out of range, while ArrayIndexOutOfBoundsException
is thrown when an array index is out of range.
By throwing an ArrayIndexOutOfBoundsException
, they're being clear about what the index is - it is specifically an array index. If they threw anIndexOutOfBoundsException
, this could refer to any index.
Tl;dr: They're being more specific and clearer.
As to why the documentation says it throws an IndexOutOfBoundsException
, but actually throws an ArrayIndexOutOfBoundsException
.
The ArrayIndexOutOfBoundsException
does extend from IndexOutOfBoundsException
, so they can be classified under the IndexOutOfBoundsException
and it's easier to simply refer to the superclass, but ultimately throws an ArrayIndexOutOfBoundsException
because it's more precise. You also use IndexOutOfBoundsException
in try/catch blocks a lot more than ArrayIndexOutOfBoundsException
, so this make it easy to catch an exception from System.arrayCopy()
, but also makes it easier to be more specific when catching the exception.
Upvotes: 1
Reputation: 521997
As you seem to already know, IndexOutOfBoundsException
is the parent class of ArrayIndexOutOfBoundsException
. I think that by returning a more generic type of exception, Java can keep its System.arraycopy()
API more flexible. Suppose in the future that another type of index bounds exception is thrown by System.arraycopy()
. If the API used the rigid ArrayIndexOutOfBoundsException
then anyone using the method might have to refactor his code. On the other hand, by using IndexOutOfBoundsException
, the code remains flexible, and new types of exceptions can be handled with minimal hassle.
So in summary I would say that Java did this out of good design practice.
Upvotes: 1