"but the compiler doesn't know this" - what's the sense?

Andremoniy :

I've met a such kind of code and comments in the java.util.ImmutableCollections class:

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
    } 
    ...
}

Why not just throw new IndexOutOfBoundsException(...)? What's the reason?

Andremoniy :

Perhaps, it is just an avoiding of a code duplication, because otherwise it would be something like:

new IndexOutOfBoundsException(outOfBoundsMessage(..., ...)) 

but outOfBounds* methods are private, so by design somebody should call wrappers like return Preconditions.checkIndex(index, length, null)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=437140&siteId=1