The toString() method of Java.lang.Throwable class used to return a String representation of this Throwable which consists of the name of the class of this object, a colon and a space(“: “) and a string which is same as result of invoking this object’s getLocalizedMessage() method and If getLocalizedMessage returns null, then just the class name is returned.
public String toString()
Return Value: This method returns String representation of this Throwable if a Exception occurs.
Below programs illustrate the toString() method of Throwable class:
Exception: java.lang.Exception: New Exception Thrown
Exception: java.lang.ArithmeticException: / by zero
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.