Throwables in Java

Let's find out the secret behind Errors, Exceptions and Throwables in Java and its Hierarchy.

Featured image

Throwables in Java

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or of one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

A Throwable class contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error.

Following is the declaration for java.lang.Throwable class −

public class Throwable
   extends Object
      implements Serializable



This class inherits methods from java.lang.Object ::

Let’s see some Constructors for Throwable class::

Sr. No. Constructor Description
1. Throwable() This constructs a new throwable with null as its detail message.
2. Throwable(String message) This constructs a new throwable with the specified detail message.
3. Throwable(String message, Throwable cause) This constructs a new throwable with the specified detail message and cause.
4. Throwable(Throwable cause) This constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).



And now let’s explore the class methods ::

Sr. No. Constructor Description
1. Throwable fillInStackTrace() This method fills in the execution stack trace.
2. Throwable getCause() This method returns the cause of this throwable or null if the cause is nonexistent or unknown.
3. String getLocalizedMessage() This method creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().
4. String getMessage() This method returns the detail message string of this throwable.
5. StackTraceElement[] getStackTrace() This method provides programmatic access to the stack trace information printed by printStackTrace().
6. Throwable initCause(Throwable cause) This method initializes the cause of this throwable to the specified value.
7. void printStackTrace() This method prints this throwable and its backtrace to the standard error stream.
8. void printStackTrace(PrintStream s) This method prints this throwable and its backtrace to the specified print stream.
9. void printStackTrace(PrintWriter s) This method prints this throwable and its backtrace to the specified print writer.
10. void setStackTrace(StackTraceElement[] stackTrace) This method sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods.
11. String toString() This method returns a short description of this throwable.



Okay, that was too much of methods and constructors, but this is the basic which is common to all Throwables. Further moving on, we will see the heirarchy of Throwable class but before that some basics on Throwable::

A Throwable instance contains the current execution stack, captured when the error exception occurred. It can also contain a message (obtained via the getMessage() method), indicating the relevant error message. Lastly, for exception chains where one error causes another error to be thrown, a Throwable can obtain a potential cause (collected via the getCause() method) as well.

Let’s dive into the hierarchy now :: Throwable class is extended by two classes :



Let’s see the hierarchy ::

I wish I could show the hierarchy more visibly but this is the most creative idea that came to my mind. Lets see it in visual format.

Error Hierarchy

Exception Hierarchy