Custom Exceptions

Java comes with many built-in exceptions (see a list of common exceptions and their description here) such as the IndexOutOfBoundsException. These exceptions cover most situations that are bound to happen in programs where there is a need to throw an exception. However, there will be times we need to supplement these built-in exceptions with our own.

Creating custom exceptions can be as simple as extending an existing built-in exception.

To learn the process, we make our own version of IndexOutOfBoundsException. We call ours simply IndexException.

  • Create a Java class IndexException with the following content:
    public class IndexException extends RuntimeException {
    
    }
    
  • Update IndexedList, IndexedListTest and ArrayIndexedList: replace any usage of IndexOutOfBoundsException with IndexException.

All built-in exceptions have a default constructor as well as an overloaded constructor that takes a String (message) as a parameter. It is considered good practice to provide a similar overloaded constructor for your custom exceptions.

/**
 * Exception for invalid index. Data structures using (integer) indices
 * throw IndexException if a given index is out of range.
 */
public class IndexException extends RuntimeException {

  /**
   * Constructs a new IndexException.
   */
  public IndexException() {
  }

  /**
   * Constructs a new IndexException with the specified detail message.
   *
   * @param message the detail message. The detail message is saved for
   *                later retrieval by the getMessage() method.
   */
  public IndexException(String message) {
    super(message);
  }
}
Resource

Create a Custom Exception in Java by Baeldung is a good read.