[英]Is there any circumstance that System.arraycopy throws IndexOutOfBoundsException?
I'm reading the JavaDoc of System.arraycopy
and found something confused me. 我正在阅读System.arraycopy
的JavaDoc ,发现有些困惑。
... if any of the following is true, an
IndexOutOfBoundsException
is thrown and the destination is not modified: ...如果满足以下任一条件,则抛出IndexOutOfBoundsException
,并且不修改目标:
- The
srcPos
argument is negative.srcPos
参数为负。- The
destPos
argument is negative.destPos
参数为负。- The
length
argument is negative.length
参数为负。srcPos+length
is greater thansrc.length
, the length of the source array.srcPos+length
大于源数组的长度src.length
。destPos+length
is greater thandest.length
, the length of the destination array.destPos+length
大于目标数组的长度dest.length
。
And in the statement of throws
, it says 在throws
的语句中,它说
IndexOutOfBoundsException - if copying would cause access of data outside array bounds. IndexOutOfBoundsException-如果复制将导致超出数组范围的数据访问。
I'm wondering... 我很好奇...
Why they use IndexOutOfBoundsException but not ArrayIndexOutOfBoundsException? 为什么他们使用IndexOutOfBoundsException但不使用ArrayIndexOutOfBoundsException?
I tried all the circumstances stated above, and all threw ArrayIndexOutOfBoundsException actually. 我尝试了上述所有情况,并且实际上都抛出了ArrayIndexOutOfBoundsException。
So is there any reason so that they avoid using the subclass of IndexOutOfBoundsException in the JavaDoc? 那么,有什么理由可以避免在JavaDoc中使用IndexOutOfBoundsException的子类?
As you seem to already know, IndexOutOfBoundsException
is the parent class of ArrayIndexOutOfBoundsException
. 如您所知, IndexOutOfBoundsException
是ArrayIndexOutOfBoundsException
的父类。 I think that by returning a more generic type of exception, Java can keep its System.arraycopy()
API more flexible. 我认为,通过返回更通用的异常类型,Java可以使其System.arraycopy()
API更加灵活。 Suppose in the future that another type of index bounds exception is thrown by System.arraycopy()
. 假设将来System.arraycopy()
抛出另一种类型的索引范围异常。 If the API used the rigid ArrayIndexOutOfBoundsException
then anyone using the method might have to refactor his code. 如果API使用了严格的ArrayIndexOutOfBoundsException
则使用该方法的任何人可能都必须重构其代码。 On the other hand, by using IndexOutOfBoundsException
, the code remains flexible, and new types of exceptions can be handled with minimal hassle. 另一方面,通过使用IndexOutOfBoundsException
,代码保持了灵活性,并且可以以最小的麻烦处理新类型的异常。
So in summary I would say that Java did this out of good design practice. 因此,总而言之,我要说Java是出于良好的设计实践而进行的。
From the javadoc: 从javadoc:
ArrayIndexOutOfBoundsException
: 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
: 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. 当任何索引超出范围时,将引发IndexOutOfBoundsException
,而当数组索引超出范围时,将引发ArrayIndexOutOfBoundsException
。
By throwing an ArrayIndexOutOfBoundsException
, they're being clear about what the index is - it is specifically an array index. 通过抛出ArrayIndexOutOfBoundsException
,他们可以清楚地知道索引是什么-特别是数组索引。 If they threw an IndexOutOfBoundsException
, this could refer to any index. 如果他们抛出IndexOutOfBoundsException
,则可以引用任何索引。
Tl;dr: They're being more specific and clearer. Tl; dr:他们变得更加具体和清晰。
As to why the documentation says it throws an IndexOutOfBoundsException
, but actually throws an ArrayIndexOutOfBoundsException
. 至于为什么文档说它抛出IndexOutOfBoundsException
却实际上抛出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. ArrayIndexOutOfBoundsException
确实从IndexOutOfBoundsException
扩展而来,因此可以在IndexOutOfBoundsException
下对其进行分类,并且更容易简单地引用超类,但最终会抛出ArrayIndexOutOfBoundsException
因为它更精确。 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. 您还可以在try / catch块中使用IndexOutOfBoundsException
,而不是ArrayIndexOutOfBoundsException
,这使从System.arrayCopy()
捕获异常更加容易,但是在捕获异常时也变得更加具体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.