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.
Exception Class Hierarchy in Java
The hierarchy of exception class in Java 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 is thrown when arithmetic problems, such as a number is divided by zero, is occurred. That is, it is caused by maths error.
The ClassCastException is a runtime exception that is thrown by JVM when we attempt to invalid typecasting in the program. That is, it is thrown when we cast an object to a subclass of which an object is not an instance.
This exception class is thrown by JVM when an array or string is going out of the specified index. It has two further subclasses:
StringIndexOutOfBoundsException exception is thrown when a String or StringBuffer element is accessed out of the index.
NullPointerException is a runtime exception that is thrown by JVM when we attempt to use null instead of an object. That is, it is thrown when the reference is null.
ArrayStoreException exception occurs when we attempt to store any value in an array which is not of array type. For example, suppose, an array is of integer type but we are trying to store a value of an element of another type.
The IllegalStateException exception is thrown by programmatically when the runtime environment is not in an appropriate state for calling any method.
This exception is thrown when a thread does not have the right to monitor an object and tries to access wait(), notify(), and notifyAll() methods of the object.
The NegativeArraySizeException exception is thrown when an array is created with a negative size.
List of Checked Exceptions in Java
Now, we have listed checked exceptions in a brief description.
The FileNotFoundException is a checked exception that is thrown when we attempt to access a non-existing file.
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!!!