Class Throwable in Java
The throwable class is the superclass of all errors and exceptions in the java that can be thrown by the throw keyword. All exceptions and errors are subclasses of the Throwable. Only objects which are an instance of this class or one of its subclass are thrown by the java virtual machine. It can also be thrown by java’s statement.
So only this class or it’s subclass can be the argument type in catch clause. A throwable object 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 or exception.
A Throwable object can have an associated message that provides more detail about the particular error or exception that is being thrown.
The Throwable class provides a method that outputs information about the state of the system when an exception object is created. This method can be useful in debugging Java programs.
The getMessage() method retrieves any error message associated with the exception or error.
printStackTrace() prints a stack trace that shows where the exception occurred.
fillInStackTrace() extends the stack trace when the exception is partially handled, and then re-thrown.
The structure of the throwable class given below:
public class java.lang.Throwable extends java.lang.Object implements java.io.Serializable{
//constructors:
public Throwable();
//creates a new throwable object with no detail message. The stack trace is automatically filled in.
public Throwable(String message);
//creates a new throwable object with the detail message provided in the argument. The stack trace is automatically filled in.
//Methods:
public native Throwable fillInStackTrace();//fills the stack trace of this Throwable Object with the current stack trace,adding to any previous information in the stack trace.
public String getMessage(); //returns the details message about the exception that has occurred. This message is initialized in the Throwable constructor.
public void printStackTrace();//prints the result of toString() along with the stack trace.
public void printStackTrace(PrintStream ps);//prints the result of toString() along with the stack trace to the Stream like System.err,the Error output stream
public String toString();//returns the name of the class concatenated with the result of getMessage().Short description of Throwable Object
public Throwable getCause();//returns the cause of the exception as represented by a Throwable Object
public stackTraceEmenet[] getStackTrace()//returns an error containing each element on the stack trace.The element at index-0 represents the top of call stack and the last element in the Array represents at the bottom of the call stack.
public void printStackTrace(PrintWriter ps)//prints the stack trace to the indicated print writer.
public String getLocalizedMessage();
}
The details of the Throwable class is given as:
public Throwable();
public Throwable() constructor creates a Throwable object with no associated message. This constructor calls fillInStackTrace() so that information is available for printStackTrace().
public Throwable(String message);
public Throwable(String message) constructor Create a Throwable object with an associated message. This constructor calls fillInStackTrace() so that information is available for printStackTrace().
Parameter
message – A message string to be associated with the object.
public native Throwable fillInStackTrace();
public native Throwable fillInStackTrace() method puts stack trace information in this Throwable object. It is not usually necessary to explicitly call this method since it is called by the constructors of the class.
However, this method can be useful when rethrowing an object. If the stack trace information in the object needs to reflect the location that the object is rethrown from, fillInStackTrace() should be called.
This method returns a reference to this object.
try{
x=y/z;
}
catch(ArithmeticThrowable e){
x=Number.MAX_VALUE;
throw e.fillInStackTrace();
}
public String getLocalizedMessage();
public String getLocalizedMessage() method creates a localized version of the message that was associated with this object by its constructor.
The getLocalizedMessage() method in Throwable always returns the same result as
getMessage().
A subclass must override this method to produce a locale-specific message.
This method returns a localized version of the String object associated with this Throwable object, or null if there is no message associated with this object.
public String getMessage();
public String getMessage() method returns any string message that was associated with this object by its constructor.
This method returns the String object associated with this Throwable object, or null if there is no message associated with this object.
public void printStackTrace();
public void printStackTrace() method outputs the string representation of this Throwable object and a stack trace to System.err.
public void printStackTrace(PrintStream ps);
public void printStackTrace(PrintStream ps) method outputs the string representation of this Throwable object and a stack trace to the specified PrintStream object.
Parameter
ps – A java.io.PrintStream object.
public void printStackTrace(PrintWriter ps);
public void printStackTrace(PrintWriter ps) method outputs the string representation of this Throwable object and a stack trace to the specified PrintWriter object.
Parameter
ps – A java.io.PrintWriter object.
public String toString();
public String toString() method returns a string representation of this Throwable object.
This method returns a string representation of this object.
Apart from these Throwable class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)
Why catching of Throwable Error is a bad idea in java?
Even though java allows to catch any instance of Java.lang.Throwable, then why it is not a good practice? The second question may arise if they are bad then why java is allowing them?
The answer is not very simple. The reasons are-
To catch an error, we need to include Java.lang.Throwable in the method signature.
Like:- public void doThis() throws Throwable. But when we declare in this way, the reader of the code does not understand what kind of problem we are talking here, as a result, coders can not provide a solution to this problem.
The objective of Exception handling is to provide a way when exceptional conditions occur. But in this context, coders can not provide a solution. It just makes the solution space tough.
Note:- It is a good practice to catch Throwable and rethrow after wrapping into a RuntimeException. This should only be done in case of logging that helps us to understand the issue better.
Consider a realtime scenario where an application is running 24*7. It should not go off even if there is an error occurred. This type of logging provides a safety net around the code to under the issue and helps to debug with a log message.