• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between ArrayIndexOutOfBoundsException and IndexOutOfBoundsException  RSS feed

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all

As we know ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException can you tell in which circumtances the boexceptions will get thrown
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API docs
Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had gone through that code but i'm not cleared can any body tell me with some code taking as an example ....i thought for arrays it will throw arrayindexoutofbound exception and for collection it will throw indexoutofboundexcetion but when i used for vector its showing arrayindexoutofbound exception only
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector (and ArrayList) use arrays under the covers to store data, so it's not surprising that you could get an ArrayIndexOutOfBoundsException when using those.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in the end, ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException (as does StringIndexOutOfBoundsException for that matter).

So if you want to catch that exception (and you shouldn't - you should check the bounds and prevent it!) you can catch IndexOutOfBoundsException and you'll also catch ArrayIndexOutOfBoundsException.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!