Last Updated on May 30, 2020 by Scientech Easy
In the earlier tutorials, we have known that all exceptions and errors in Java are represented by classes. All these classes are organized as subclasses in a hierarchy under superclass called Throwable.
Throwable in Java is a class that is the superclass of all exceptions and errors which may occur in Java program. It extends object class. Therefore, throwable class is itself a subclass of superclass of all Java classes, Object class.
Throwable Class Hierarchy in Java
Throwable class is the top most class in the exception class hierarchy but it is also immediate subclass of object class that is located at the root of exception hierarchy. The throwable class hierarchy is shown in the below diagram.
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.
Methods of Throwable class in Java
All exception classes including user-defined class use all the methods provided by Throwable class. Exception class does not define its own any methods.
Java provides the following methods in the Throwable class that can be used to retrieve and know details of an exception. An important list of methods present in Throwable class is as follows:
1. getMessage(): The getMessage() method returns detail message of exception that has recurred. The syntax for getMessage() is as follows:
public String getMessage()
The detail message is initialized in the throwable constructor that can be displayed using the following statement:
Since getMessage() method returns a string, we can store string message returned by getMessage() method in a String variable and the content of string variable can be displayed using the following statement:
String str = e.getMessage(); System.out.println(str);
2. toString(): The toString() method returns information about an exception in the form of string. The throwable class overrides toString() method of object class. The information can be displayed on the screen using the println() method. The following syntax can be used:
public String toString()
System.out.println(e.toString()); or, String str = e.toString(); // Here, str is a reference variable that stores information about an exception returned by toString() method in the form string. System.out.println(str);
3. printStackTrace(): This method prints the stack trace of an exception. It gives information about where the exception occurs in the case of nested method calls. The syntax for printStackTrace() method is as follows:
public void printStackTrace() // It does not return anything. For example: e.printStackTrace(); // e is an exception type variable.
4. fillInStackTrace(): This method returns a Throwable object that contains a completed stack trace. The object can be rethrown. The syntax for fillInStackTrace() method is given below:
public Throwable fillInStackTrace()
5. getStackTrace(): The getStackTrace() method returns an array that contains each element on the stack trace. The element at index 0 represents the top of call stack and the last element represents the bottom of call stack. The syntax is as follows:
public StackTraceElement getStackTrace()
6. getClause(): The getClause() method returns the exception that caused the occurrence of current exception. If there is no caused exception then null is returned. The syntax is as follows:
public Throwable getClause()
7. initCause(): The initCause() method joins “causeExc” with the invoking exception and returns a reference to the exception. The syntax is given below:
public Throwable initCause(Throwable causeExc)
Java language provides a hierarchy of classes that represent different types of exceptions. These classes are derived from java.lang.Throwable class. Throwable class in Java defines several useful methods that can be used for getting and knowing the details of an exception. Hope that this tutorial has covered almost all important points related to Throwable class in Java.
Thanks for reading!!!