Types of Exceptions in Java
Basically, there are two types of exceptions in java. They are:
1. Predefined Exception (Built-in-Exceptions)
2. Custom Exceptions
Predefined exceptions are those exceptions that are already defined by Java system. These exceptions are also called built-in-exceptions.
Java supports exception handling by providing the number of predefined exceptions. These predefined exceptions are represented by classes in java.
When a predefined exception occurs, JVM (Java runtime system) creates an object of predefined exception class. All exceptions are derived from java.lang.Throwable class but not all exception classes are defined in the same package.
All the predefined exceptions supported by java are organized as subclasses in a hierarchy under the Throwable class.
The Throwable class is the root of exception hierarchy and is an immediate subclass of Object class. Let’s take a look at the java exception hierarchy, as shown in the below figure.
1. Throwable class: As shown in the above figure, Throwable class which is derived from Object class, is a top of exception hierarchy from which all exception classes are derived. It is present in java.lang package.
Throwable class is the superclass of all exceptions in java. This class has two subclasses: Error and Exception. Errors or exceptions occurring in java programs are objects of these classes. Using Throwable class, you can also create your own custom exceptions.
2. Error: Error class is the subclass of Throwable class and a superclass of all the runtime error classes. It terminates the program if there is problem-related to a system or resources (JVM).
An error generally represents an unusual problem or situation from which it is difficult to recover. It does not occur by programmer mistakes. It generally occurs if the system is not working properly or resource is not allocated properly.
VirtualMachineError, StackOverFlowError, AssertionError, LinkageError, OutOfMmeoryError, etc are examples of error.
3. Exception: Exception class is a subclass of Throwable class and a superclass of all the exception classes. All the exception classes are derived directly or indirectly from the Exception class. They generally originate from within the application.
The exception class provides two constructors:
a. public Exception() (Default constructor)
b. public Exception(String message) (It takes a message string as argument)
Each of exception classes provides two constructors: one with no argument and another with a String type argument. Exception class does not provide its own method. It inherits all methods provided by Throwable class.
The hierarchy of exception class has been shown in the below figure that is very important for an interview purpose.
RuntimeException class (Unchecked Exception)
RuntimeException class is a subclass of the Exception class. It is thrown by JVM or programmatically when an arithmetic operation performed in the program is incorrect or defect/bug occurs in the program’s code.
RuntimeException and all its exception subclasses are not checked by Java compiler because they occur during runtime of a program. That’s why these exceptions are also called unchecked exceptions.
RuntimeException class consists of many other exception subclasses that are used to handle a specific type of exception. Apart from these exception subclasses of RuntimeException class shown in the above figure, there are also other subclasses of RuntimeException class which has not been shown in the hierarchy structure diagram to avoid complexity.
Let’s see a brief description of them.
This exception class is thrown by JVM when an array or string is going out of the specified index. It has two further subclasses:
List of Checked Exceptions
Now, we have listed checked exceptions in a brief description.
Hope that this tutorial has covered almost all the basic points related to the exception hierarchy in java. I hope that you will have understood the basic points of Throwable class and its subclasses: Exception and Error.
Thanks for reading!!!