Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

charAt(): Why can't it throw StringIndexOutOfBoundsException?

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From one of Enthuware's mock exams:

What will the following code print?



The answer being IndexOutOfBoundsException.

The explanation is as follows:

As per the API documentation for charAt, it throws IndexOutOfBoundsException if you pass an invalid value (that is, if the index argument is negative or not less than the length of this string).

Both - ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException, extend IndexOutOfBoundsException and although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException.



What does it mean that the implementation is free to throw another exception, as long as it's a subclass of IndexOutOfBoundsException? Does it mean that an overriding charAt() method (created by the programmer) may throw any subclass of IndexOutOfBoundsException?

Thanks again.

Regards,
Shane
 
Enthuware Software Support
Posts: 4450
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is a final class so this method cannot be overridden. By "implementation is is free to throw another exception, as long as it's a subclass of IndexOutOfBoundsException", it means that the JDK class code would be well within its rights if decides to throw some other subclass of IndexOutOfBoundsException

Here is a thread that discusses this issue -

http://enthuware.com/forum/viewtopic.php?f=2&t=1587

The answer to this question is admittedly controversial but we have still kept it to make sure the reader is aware of the issue (charAt method is included in the objectives and there are questions in the exam on this method).
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:What does it mean that the implementation is free to throw another exception, as long as it's a subclass of IndexOutOfBoundsException?


Exactly!

Shane Jensen wrote:Does it mean that an overriding charAt() method (created by the programmer) may throw any subclass of IndexOutOfBoundsException?


Do you remember your question about Negative index for ArrayList element leading to ArrayIndexOutOfBoundsException? It's exactly the same story here! Except that the interface here is not List but CharSequence; the method is not get() but charAt(); and the subclass is not ArrayIndexOutOfBoundsException but StringIndexOutOfBoundsException.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Shane Jensen wrote:What does it mean that the implementation is free to throw another exception, as long as it's a subclass of IndexOutOfBoundsException?


Exactly!

Shane Jensen wrote:Does it mean that an overriding charAt() method (created by the programmer) may throw any subclass of IndexOutOfBoundsException?


Do you remember your question about Negative index for ArrayList element leading to ArrayIndexOutOfBoundsException? It's exactly the same story here! Except that the interface here is not List but CharSequence; the method is not get() but charAt(); and the subclass is not ArrayIndexOutOfBoundsException but StringIndexOutOfBoundsException.


Hi Roel,

Thanks for the comparison.
    Bookmark Topic Watch Topic
  • New Topic