Throwable toString() method in Java with Examples Last Updated : 06 Feb, 2019 Suggest changes Share 2 Likes Like Report 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. Syntax: 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: Example 1: Java // Java program to demonstrate // the toString() Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { testException(); } catch (Throwable e) { // print using tostring() System.out.println("Exception: " + e.toString()); } } // method which throws Exception public static void testException() throws Exception { throw new Exception("New Exception Thrown"); } } Output: Exception: java.lang.Exception: New Exception Thrown Example 2: Java // Java program to demonstrate // the toString() Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { // divide two numbers int a = 4, b = 0; int c = a / b; } catch (Throwable e) { // print using tostring() System.out.println("Exception: " + e.toString()); } } } Output: Exception: java.lang.ArithmeticException: / by zero References: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#toString() A AmanSingh2210 Follow 2 Article Tags : Java Java-Exceptions Java-lang package Java-Exception Handling Java-Functions java-Throwable +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like