Exceptions Hierarchy in Java

In this article, we will learn the exceptions class hierarchy in java.lang package. The objects that inherit from the Throwable class include direct descendants (objects that inherit directly from the Throwable class) and indirect descendants (objects that inherit from children or grandchildren of the Throwable class).
Throwable has two direct descendants:
  1. Error Class
  2. Exception Class
The figure below illustrates the class hierarchy of the Throwable class and its most significant subclasses.

1. Error Class

When a dynamic linking failure or other hard failures in the Java virtual machine occurs, the virtual machine throws an Error. Simple programs typically do not catch or throw Errors.
The figure below illustrates the class hierarchy of Error Class.

2. Exception Class

Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not a serious system problem. Most programs you write will throw and catch Exceptions as opposed to Errors.
The Java platform defines the many descendants of the Exception class. These descendants indicate various types of exceptions that can occur.
The figure below illustrates the class hierarchy of the Exception Class.

Exception Class Example:
FileNotFoundException exceptions are good examples for Exception class. In this example, we will use the FileNotFoundException exception type to demonstrate the usage of the Exception class.
In this example, FileReader class try to read a file from an invalid location will throw FileNotFoundException exception.
public class FileNotFoundExceptionExample {
    public static void main(String[] args) {
        
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(new File("/invalid/file/location")));
        } catch (FileNotFoundException e) {
            System.err.println("FileNotFoundException caught!");
        }
    }
}
Output:
FileNotFoundException caught!

RuntimeException Class

One Exception subclass, RuntimeException, is reserved for exceptions that indicate an incorrect use of an API. An example of a runtime exception is NullPointerException, which occurs when a method tries to access a member of an object through a null reference.
The figure below illustrates the class hierarchy of the RuntimeException Class.
RuntimeException Example:
Let's consider NullPointerException is the best example for runtime exception. In below example, Person class object is not created using the new keyword but it is just declared with a null value. Now we are trying to access personName field value on null reference so JVM will throw NullPointerException exception here.
public class NullPointerExceptionExample {

    public static void main(String[] args) {

        Person personObj = null;

        try {
            String name = personObj.personName; // Accessing the field of a null object
            personObj.personName = "Jon Doe"; // Modifying the field of a null object
        } catch (NullPointerException e) {
            System.err.println("NullPointerException caught!");
        }

    }
}

class Person {

    public String personName;

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

}
Output:
NullPointerException caught!

Exception Handling Related Posts

Comments