20

I'm trying to check if an array location is out of bounds, what's the simplest way?

int[] arr;
populate(arr);
if(arr[-1] == null)
//out of bounds!

Would something like this work?

I'm pretty sure this can be done with a trycatch or a scanner but for just a simple small program, is there another way?

dukevin
  • 22,384
  • 36
  • 82
  • 111

3 Answers3

48

Absolutely do not use try-catch for this. Simply use:

boolean inBounds = (index >= 0) && (index < array.length);

Implementing the approach with try-catch would entail catching an ArrayIndexOutOfBoundsException, which is an unchecked exception (i.e. a subclass of RuntimeException). Such exceptions should never (or, at least, very rarely) be caught and dealt with. Instead, they should be prevented in the first place.

In other words, unchecked exceptions are exceptions that your program is not expected to recover from. Now, there can be exceptions (no pun intended) to this here and there. For instance, it has become common practice to check if a string is parable as an integer by calling Integer.parseInt() on it and catching the potential NumberFormatException (which is unchecked). This is considered OK, but always think twice before doing something like that.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    Is the system in an illegal state with unpredictable results? What exactly is the harm? – durron597 Oct 07 '14 at 13:40
  • I believe in most cases one should only catch unchecked exceptions when IO is involved, for instance in the case of converting a String to an Integer or check for null input from a hardware device (this often includes working with JNI). Otherwise you should avoid catching uncheck exceptions as suggested. Adding a comment to provide further context to what was suggested. – Alex Savenok Jun 02 '17 at 15:30
5

No, that will cause an exception.

Instead, do

if (x < 0 || x >= arr.length) {
    //x is out of bounds!
}
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 2
    I believe it's `x >= arr.length` – Josh M Oct 30 '13 at 01:40
  • And don't forget the `()` brackets around the condition. – arshajii Oct 30 '13 at 01:40
  • 1
    what if arr is empty and x is zero it will fail – Braj Jun 12 '19 at 12:50
  • Yes! This is the correct answer to check if x is out of bounds: if (x < 0 || x >= arr.length). To check if it's IN bounds, we perform a simple negation: if (! (x < 0 || x >= arr.length)) -- which, after simplifying using De Morgan's Law, we obtain: if (x >= 0 && x < arr.length). Final note: Do NOT use/abuse Exceptions! – ONE Dec 04 '22 at 17:13
3

It is better to check if the index is in range manually.

if(index >=0 && index < array.length){
   // in range
}  

Exceptions which are unchecked, that is which do not require a try-catch block, should not be caught. ArrayIndexOutOfBoundsException is one of them.

All unchecked exceptions come from RuntimeException class.
From this class comes the IndexOutOfBoundsException
This is further specialized into StringIndexOutOfBounds and ArrayIndexOutOfBounds

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

An SO User
  • 24,612
  • 35
  • 133
  • 221