Different Ways to Print Exception Messages in Java - GeeksforGeeks

06 Dec, 2021
In Java, there are three methods to print exception information. All of them are present in the Throwable class. Since Throwable is the base class for all exceptions and errors, we can use these three methods on any exception object. 
There are three methods to print exception messages in Java. These are:
1. java.lang.Throwable.printStackTrace() method: 
By using this method, we will get the name(e.g., java.lang.ArithmeticException) and description(e.g., / by zero) of an exception separated by a colon, and the stack trace (wherein the code, that exception has occurred) in the next line. 
Syntax: 
public void printStackTrace()
Java
// Java program to demonstrate// printStackTrace method  public class Test {    public static void main(String[] args)    {        try {            int a = 20 / 0;        }        catch (Exception e) {            // printStackTrace method            // prints line numbers + call stack            e.printStackTrace();              // Prints what exception has been thrown            System.out.println(e);        }    }}
Runtime Exception: 
java.lang.ArithmeticException: / by zero
    at Test.main(Test.java:9)
Output:
java.lang.ArithmeticException: / by zero
2. toString() method: 
Using this method will only get the name and description of an exception. Note that this method is overridden in the Throwable class. 
Java
// Java program to demonstrate// toString  method  public class Test {    public static void main(String[] args)    {        try {            int a = 20 / 0;        }        catch (Exception e) {            // toString method            System.out.println(e.toString());              // OR            // System.out.println(e);        }    }}
java.lang.ArithmeticException: / by zero
3. java.lang.Throwable.getMessage() method: 
Using this method, we will only get a description of an exception. 
Syntax: 
public String getMessage()
Java
// Java program to demonstrate// getMessage method  public class Test {    public static void main(String[] args)    {        try {            int a = 20 / 0;        }        catch (Exception e) {            // getMessage method            // Prints only the message of exception            // and not the name of exception            System.out.println(e.getMessage());              // Prints what exception has been thrown            // System.out.println(e);        }    }}
/ by zero
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
nishkarshgandhi
Exception Handling
Java-Exceptions
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Interfaces in Java
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Overriding in Java
Stack Class in Java
Collections in Java