Page 1 of 1

[HD-OCP17/21-Fundamentals Pg 401, Sec. 15.2.2 - common-exceptions-that-are-usually-thrown-by-the-jvmexceptions-thrown-by

Posted: Sun Jan 05, 2025 8:17 pm
by raphaelzintec
Error in page 436

Code: Select all

int[] ia = new int[]{ 1, 2, 3}; // ia is of length 3
System.out.println(ia[-1]); //ArrayIndexOutOfBoundsException
System.out.println(ia[3]); //ArrayIndexOutOfBoundsException
System.out.println("0123".charAt(4)); //StringIndexOutOfBoundsException
Page 300
char charAt(int index) - Returns the char value at the specified index. Throws IndexOutOfBoundsException if the index argument is negative or is not less than the length of this string.

Java doc says charAt throws IndexOutOfBoundsException :D

Re: [HD-OCP17/21-Fundamentals Pg 401, Sec. 15.2.2 - common-exceptions-that-are-usually-thrown-by-the-jvmexceptions-throw

Posted: Sun Jan 05, 2025 8:40 pm
by admin
It actually throws ArrayIndexOutOfBoundsException.

Re: [HD-OCP17/21-Fundamentals Pg 401, Sec. 15.2.2 - common-exceptions-that-are-usually-thrown-by-the-jvmexceptions-throw

Posted: Sun Jan 05, 2025 9:05 pm
by raphaelzintec
System.out.println("0123".charAt(4));
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4

Re: [HD-OCP17/21-Fundamentals Pg 401, Sec. 15.2.2 - common-exceptions-that-are-usually-thrown-by-the-jvmexceptions-throw

Posted: Sun Jan 05, 2025 9:06 pm
by raphaelzintec
but java doc says IndexOutOfBoundsException

so now either page 436 has error either 300

Re: [HD-OCP17/21-Fundamentals Pg 401, Sec. 15.2.2 - common-exceptions-that-are-usually-thrown-by-the-jvmexceptions-throw

Posted: Sun Jan 05, 2025 9:31 pm
by admin
Sorry, my bad, the code in charAt method throws SIOOBE. Since SIOOBE extends IOOBE, the contract published by the JavaDoc is satisfied.
The book correctly explains this, "Methods of String class throw another of its subclass StringIndexOutOfBoundsException when you try to access a character at an invalid index."