我在 java.util.ImmutableCollections
类中遇到了这样一种代码和注释:
static final class List0<E> extends AbstractImmutableList<E> {
...
@Override
public E get(int index) {
Objects.checkIndex(index, 0); // always throws IndexOutOfBoundsException
return null; // but the compiler doesn't know this
}
...
}
为什么不直接使用
throw new IndexOutOfBoundsException(...)
?原因是什么?