title: Try catch for ArrayIndexOutOfBoundsException
body: <p>I am running a test for Binary search method for a value which is not found in an object array by using the try catch block, but the error <code>ArrayIndexOutOfBoundsException</code> is still being thrown.</p>
  <p>Please refer to below code</p>
  <pre><code>int high = foodlist.length - 1;
  int result = foodObj.binarySearch(foodlist, 0, high , key);
  
      try {
          if (result != -1)
              System.out.println(&quot;Element is found at index: &quot; + result);
          else
              throw new ArrayIndexOutOfBoundsException();
      } 
      catch (ArrayIndexOutOfBoundsException e) {
          System.out.println(&quot;Element not found!&quot;);
          System.out.println(&quot;Please enter an ID in the range of 1 to 3&quot;);
      }
  
      // Binary Search method
  public static int binarySearch(Food[] foodlist, int low, int high, int value) {
      
      if (high &gt;= low) {
          int mid = low + (high - low) / 2;
          
          if (foodlist[mid].id == value)
              return mid;
          
          else if (foodlist[mid].id &gt; value)
              return binarySearch(foodlist, low, mid-1, value);
          else 
              return binarySearch(foodlist, high, mid+1, value);
      }
      return -1;
      
  }
  </code></pre>
  <p>Maybe I should change the logic <code>return binarySearch(foodlist, high, mid+1, value);</code>, but I have tried all possible combination, same out of bound error is thrown or not found for valid elements is displayed.</p>
  <p>Any help would be much appreciated, Thanks</p>