Custom Exceptions
Java comes with many built-in exceptions (see a list of most used 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 IndexException
.
- Create a Java class
IndexException
with the following content:public class IndexException extends RuntimeException { }
- Update
IndexedList
,IndexedListTest
andArrayIndexedList
: replace any usage ofIndexOutOfBoundsException
withIndexException
.
All built-in exceptions have a default constructor and 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.