• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to make a check for index out of bound Exception  RSS feed

 
preethi venkatarangan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If have a ArrayList & i get An IndexOut Of bound Exception.
is there a way i could check/handle it .that is if this exception occurs then do this else continue.


Thanks and Regards
Madhavi.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put your code in a try/catch block as you would with any other exception. Just because it's a RuntimeException doesn't mean it can't be caught, just that it doesn't have to be.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But correct use of for loops, while (myIterator.hasNext()), etc, ought to ensure that an ArrayIndexOutOfBoundsException never occurs.

The one thing you never do is this
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should only get an (Array)IndexOutOfBoundsException in ArrayList if you're using a method with a direct index, and that index is either a) < 0, or b) >= list.size()

So if you check on those bounds, you should be safe with get, remove, insert etc.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!