Java Programs with Exceptions

Throw an Exception!

Let’s update ArrayIndexedList.get to throw the IndexOutOfBoundsException:

public T get(int index) throws IndexOutOfBoundsException {
  if (index >= 0 && index < length()) {
    return data[index];
  } else {
    throw new IndexOutOfBoundsException();

Make note of the syntax; in particular, be careful with throw v.s. throws keywords.

Run IndexedListTest and see that all tests pass.