Hacker News new | past | comments | ask | show | jobs | submit login

Agreed. Though catching and handling that exception maybe more performant if majority of those indexes are within the bounds of the array.

>> I'm not sure why having the separate exception is better than letting the array access throw directly?

(1) The original String charAt method (Sun/Oracle) had to account for an offset and an index. The exception from the bounds check on the underlying char[] would be misleading/confusing... and therefore the birth of the seperate StringIndexOutOfBoundsException

Source: http://www.docjar.com/html/api/java/lang/String.java.html

(2) Because of the above and for backward compatibility, the OpenJDK is doing the same exception handling... sometimes it pays to review the original source code to get some context and rationale to the OpenJDK version.




Oh that makes sense, I had forgotten about the old substring sharing optimization.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: