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 interface class

  • 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;

Related Posts

Purpose of Throwable Class in JDK admin Core Java
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...
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 <a href="http://www.javaexperience.com/try-with-resources-and-multi-catch-in-jdk-7/" title="Exception Handling features in JDK 7">JDK</a> and why it is a class: <h2>Purpose of Throwable <del datetime="2013-01-09T12:22:41+00:00">interface</del> class</h2> <ul> <li>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.</li> <li>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</li> <li>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.</li> <li>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.</li> <li>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)</li> </ul> <h2>When to use Throwable in code</h2> 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. <h2>Throwable and Serializable</h2> 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 <a href="http://www.javaexperience.com/java-externalizable-vs-serializable-interfaces/" title="Externalizable vs Serializable Interfaces">Serializable interface</a>. 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: <Code> static final long serialVersionUID = -3387516993124229948L; </code>
Java Bytecode tutorial
What is the difference between Iterator and ListIterator
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments