VarHandle get* and set* methods lack throws IndexOutOfBoundsException documentation

Philippe Marschall kustos at gmx.net
Tue Dec 2 18:23:40 UTC 2025


Hello

The VarHandle #get* and #set* methods currently document the following 
three possible exceptions in their Javadoc

- ClassCastException
- WrongMethodTypeException
- UnsupportedOperationException, only for #set*

However, when a VarHandle was created using 
MethodHandles#byteArrayViewVarHandle or 
MethodHandles#byteBufferViewVarHandle and the index, the second 
argument, is incorrect then an IndexOutOfBoundsException is thrown. At 
some point between JDK 11 and JDK 17 a #byteArrayViewVarHandle was 
changed so that ArrayIndexOutOfBoundsException is thrown.

The following code is an example:

VarHandle LONG_BA_BE = 
MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.BIG_ENDIAN);
byte[] array = new byte[] {8, 7, 6, 5, 4, 3, 2, 1};
long l = (long) LONG_BA_BE.get(array, -1);
System.out.println(l);

Cheers
Philippe


More information about the core-libs-dev mailing list