Purpose of Throwable Class in JDK
The Throwable class resides at the top of exception handling hierarchy and all other classes like Error, Exception and RuntimeException are its sub classes. Though it feels like Throwable should be an interface like Serializable and Cloneable but it is not. Throwable is a class in Java. There are a number of reasons for which Throwable has been made a class in Java. In this tutorial, we shall those reasons as well look at sample code for using the Throwable class in a program. So let us first have a look at why Throwable is a present in JDK and why it is a class:
Purpose of Throwable
- With every exception, some kind of state is associated like the exception stack trace and the exception message and hence each exception should be a class.
- Some fellow developers would think that the Exception class could have been made as base class for exception hierarchy. But Throwable has been introduced to give the notion of something which can be thrown by one method to another. The term throw is taken from C++ programming language. This is the reason for having the name Throwable
- Had Throwable been made an interface, the intention would have been to use the generic reference of type Throwable to refer to concrete classes like NullPointerExcrption or ClassNotFoundException. As stated in the first point above, the exceptions should always be state-ful and interfaces can’t have state information.
- There are many concrete methods being defined in the Throwable class like getMessage, fillInStackTrace and printStackTrace etc. for which the Throwable had to be a class and not interface.
- Apart from Exception class, Error class has also been made to extend from Throwable class. Since Errors and Exceptions are exclusive in terms of their handling but are raised in case erroneous scenario, they need to have some super type named Throwed, Thrown or Throwable. Throwable word seemed most suitable to Java API designer (specially James Gosling)
When to use Throwable in code
Throwable is not intended to be used in application code as it is very generic. A Throwable can be checked exception, unchecked exception or an error. In any application, we need to deal with the above three and not Throwable instance as such. The methods from Throwable class could be used in the JDK as well custom exceptions. For example there is getMessage defined in Throwable class which is used so many times when doing exception handling. Another important point worth noting is that the catch handlers for exception need a Throwable type object to be passed as argument.
The Throwable class should also not be used as generic reference to concrete exception/error objects as exception handling should be as explicit as possible.
Throwable and Serializable
Since exceptions need to be passed across JVM’s specially in case of distributed environments, they need to be Serializable so that they can be transferred on the network. Hence the need to make the Throwable class implement the Serializable interface. In fact if we look at the source code of Throwable class, we can see that readObject and writeObject have been added to it to provide custom serialization. Even the Throwable class provides a custom serialVersionUID as:
static final long serialVersionUID = -3387516993124229948L;