Tutorialspoint.dev

3 Different ways to print Exception messages in Java

In Java, there are three methods to print an exception information. All of them are present in Throwable class. Since Throwable is the base class for all exceptions and errors, so we can use these three methods on any exception object.

  1. java.lang.Throwable.printStackTrace() method : By using this method, we will get name(e.g. java.lang.ArithmeticException) and description(e.g. / by zero) of an exception separated by colon, and stack trace (where in the code, that exception has occurred) in the next line.
    Syntax:

    public void printStackTrace()
    // 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 : By using this method, we will only get name and description of an exception. Note that this method is overridden in Throwable class.
    // 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);
            }
        }
    }

    Output:



    java.lang.ArithmeticException: / by zero
    
  3. java.lang.Throwable.getMessage() method : By using this method, we will only get description of an exception.
    Syntax:

    public String getMessage()
    // 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);
            }
        }
    }

    Output:

    / by zero
    

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter