Exception Hierarchy Java: Java has been around the programming world for a while. Even though other languages with more advanced features have gained popularity, Java is still widely used and isn’t going away anytime soon.
There are various concepts to grasp when learning Java, including Java exceptions. In Java, an exception is an event that interrupts the normal flow of a program while it is running. Exceptions can happen during the program’s compilation or execution, and they come in different types. These exceptions are undesirable and follow a basic hierarchy.
Exception Hierarchy In Java
In Java, exceptions are organized in a hierarchy. It starts with the Throwable class, directly connected to the Object class. The Exception class is a part of this hierarchy. The Throwable class has two subclasses: Error and Exception. To help visualize the hierarchy better, here’s a flowchart.
Before going further, let us try to understand some basic concepts about the Throwable, Error, and Exception classes.
The figure above illustrates that the Throwable class, which comes from the Object class, is at the top of the exception hierarchy. All exception classes in Java are directly or indirectly derived from this class. It is the base for all exception classes and can be found in java.lang package.
The Throwable class is the parent class for all exceptions in Java. It has two subclasses: Error and Exception. When errors or exceptions happen in Java programs, they are represented as objects of these classes. Additionally, you can create your own custom exceptions using the Throwable class.
The Error class is a subclass of the Throwable class and serves as the parent class for all runtime error classes. The program is terminated if there is a problem with the system or resources (JVM).
Errors typically represent uncommon issues or situations that are challenging to recover from. They don’t occur due to mistakes made by programmers. Usually, errors happen when the system isn’t functioning correctly or resources are not allocated properly.
Examples of errors include VirtualMachineError, StackOverflowError, AssertionError, LinkageError, OutOfMemoryError, and more.
The Exception class represents errors that can happen due to issues within the program or external factors. It is a subclass of the Throwable class and serves as the parent class for all exception classes. All the exception classes are directly or indirectly derived from the Exception class.
The Exception class in Java includes various types of exceptions that you may encounter while programming. These exceptions have specific names like NullPointerException, ClassNotFoundException, and IllegalArgumentException.
The exception hierarchy in the Java diagram displayed below illustrates the hierarchy of standard exception and error classes in Java:
You can see several exceptions, but in this post, we will not discuss them here. In this post, we will mainly focus on the Java Exception Hierarchy because we try to make the Exception Hierarchy Java post simple and easy for you.
We hope this short and simple post helps you understand the Exception Hierarchy in Java. We also believe that you learned about the throwable, Exception, and Error Classes.
We would love to hear from you! If you have any questions or comments about the topic we discussed in this blog post, please feel free to comment in the comment section below. We’ll be happy to provide further clarification or discuss this with you. Your feedback is valuable to us!