While Java has built-in exceptions, it is also possible to create your own custom subclasses that extend the Exception class. Custom classes are incredibly powerful and useful because they can be tailored to a specific application.
How to Make a Custom Exception in Java
To create a subclass of Exception, you need to create a class that extends the Exception class. As a result, your Exception class inherits all of the methods declared in the Throwable class (because Exception is a subclass of Throwable). You can, of course, override those methods to perform a more tailored task.
Custom exceptions don't need to implement any parent methods to be used in a program. Adding implementation can be helpful to give users more detail when a custom Exception is thrown or caught.
Example Custom Exception in Java
Below is an example of creating a very simple custom Exception that you can throw if/when a classroom is full.
class ClassroomFullException extends Exception {
@Override
public String toString() {
return "ClassroomFullException{ This classroom is full}";
}
}
Now here's an example of using that custom Exception:
class Main {
public static void main(String[] args){
try {
Student someStudent = new Student();
Classroom room = new Classroom();
room.size = 20;
addStudentToClassroom(someStudent, room);
} catch (ClassroomFullException exc) {
System.out.println("The classroom is full.");
System.out.println(exc.toString());
}
}
public static void addStudentToClassroom(Student student, Classroom room)
throws ClassroomFullException {
if (room.numStudents <= room.size) {
room.addStudent(student);
} else {
// manually throw new custom exception
throw new ClassroomFullException();
}
}
}
class ClassroomFullException extends Exception {
@Override
public String toString() {
return "ClassroomFullException{ This classroom is full}";
}
}
class Student{
// ...
}
class Classroom{
int size;
int numStudents;
public void addStudent(Student student) {
// ...
numStudents++;
}
// ...
}
Here's another example that all of those in the United States will understand.
public class UnderAgeException extends Exception {
@Override
public String toString() {
return "UnderAgeException{ Must be 21 to purchase alcohol }";
}
}
Now here's an example of using the UnderAgeException custom Exception:
public class Example {
public static void main(String[] args){
try {
Person somePerson = new Person(19);
Wine vino = purchaseWine(somePerson, "red");
} catch (UnderAgeException exc) {
System.out.println("You must be 21 to purchase alcohol");
}
}
public static Wine purchaseWine(Person person, String style)
throws UnderAgeException {
if (person.age >= 21) {
return new Wine(style);
} else {
// manually throw new custom exception
throw new UnderAgeException("underage");
}
}
}
You can create as many custom exceptions to handle as many custom cases as you need.
Summary: What is a Java Custom Exception
- A custom exception is a subclass of
Exception - Subclasses of
Exceptioninherit all methods fromThrowable - Custom exceptions do not need to implement any functionality