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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It actually throws ArrayIndexOutOfBoundsException.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

System.out.println("0123".charAt(4));
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

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

Post by raphaelzintec »

but java doc says IndexOutOfBoundsException

so now either page 436 has error either 300

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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."

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests