On Fri, 2 Dec 2022 12:44:18 GMT, Sergey Tsypanov <stsypa...@openjdk.org> wrote:

> I found out that this code
> 
> public class Main {
>     public static void main(String[] args) {
>         String s = "Hello world!";
>         char[] chars = s.toCharArray();
>         int point = Character.codePointAt(chars, -1, 1);
>     }
> }
> 
> throws `ArrayIndexOutOfBoundsException` instead of JavaDoc-specified 
> `IndexOutOfBoundsException`: 
> 
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 
> out of bounds for length 12
>       at java.base/java.lang.Character.codePointAtImpl(Character.java:9254)
>       at java.base/java.lang.Character.codePointAt(Character.java:9249)
>       at org.example.Main.main(Main.java:7)
> 
> and the method doesn't check whether `index` parameter is negative:
> 
> public static int codePointAt(char[] a, int index, int limit) {
>     if (index >= limit || limit < 0 || limit > a.length) {
>         throw new IndexOutOfBoundsException();
>     }
>     return codePointAtImpl(a, index, limit);
> }
> 
> I suggest to check the `index` parameter explicitly instead of relying on 
> AIOOBE thrown from accessing the array with negative index.

It does meet the spec (subclasses of exception types are ok), but doesn't meet 
expectations of the implementation, especially since the implementation has an 
explicit test and throw.

As for the test, checking for exact exception type is fine.  
JTreg tests frequently test the implementation, not just the spec.

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

PR: https://git.openjdk.org/jdk/pull/11480

Reply via email to