RFR: 8346657: Improve out of bounds exception messages for MemorySegments [v10]

Chen Liang liach at openjdk.org
Thu Nov 27 19:21:53 UTC 2025


On Fri, 21 Nov 2025 17:26:59 GMT, Igor Rudenko <duke at openjdk.org> wrote:

>> Logic for creating IndexOutOfBoundsException in MemorySegment is reworked:
>> - separate logic of checking bounds and constructing exception messages for both `access` and `slice` cases 
>> - idea presented in [JDK-8288534](https://bugs.openjdk.org/browse/JDK-8288534) slightly reworked with preservation of the original approach
>
> Igor Rudenko has updated the pull request incrementally with one additional commit since the last revision:
> 
>   implement re-throwing approach according to reviewer recomandation

This should be fine, though compilers might prefer separate not-inlined methods for throwing exceptions, so the `catch` block may do  something like:

    @ForceInline
    void checkSliceBounds(long offset, long length) {
        try {
            checkBounds(offset, length);
        } catch (IndexOutOfBoundsException e) {
            throwOutOfBounds(offset, length, true);
        }
    }

    @DontInline
    void throwOutOfBounds(long offset, long length, boolean slice) {
            String msg = String.format("Out of bound access on segment %s; attempting to %s of length %d at offset %d " +
                    "which is outside the valid range 0 <= offset+length < byteSize (=%d)", this, slice ? "get slice" : "access an element", length, offset, this.length);
            throw new IndexOutOfBoundsException(msg);
        }
    }


So the inlined `checkSliceBounds`/`checkAccessBounds` is smaller.

-------------

Marked as reviewed by liach (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/28124#pullrequestreview-3516725181


More information about the core-libs-dev mailing list