Different ways to get the exception message in Java

In the Throwable class of Java, three methods are given to get the exception message from catch blocks. Throwable class is the superclass of all exceptions.

The three methods of Throwable class to print exception object stack trace are:-
1) printStackTrace()
2) toString()
3) getMessage()

These methods are useful when we write catch block() with super class as parameter.


Exception messages displayed by the JVM

By default, the JVM displays the name of the thread, exception name, the reason for the exception and line/place of the exception where it occurs.

The exception message format, displayed by JVM:-

Exception in thread <thread-name> <exception-name> :
<reason for the exception> <place of the exception>


printStackTrace()

The printStackTrace() method of java.lang.Throwable print exception name, the reason for the exception and line/place of the exception where it occurs. It doesn’t display thread name where the exception occurs. The prototype of the printStackTrace() method is:- public void printStackTrace()

The exception message format, displayed by printStackTrace():-
Exception name: <reason of the exception> <place of the exception>

Example of displaying Exception message using printStackTrace() method,

catch(Exception e) {
     e.printStackTrace();
 }

toString()

The toString() method is an overridden method in the java.lang.Throwable class, its return type is String. It returns the exception name, and reason of the exception as a string.

The exception message format, displayed by toString():-
Exception name: <reason of the exception>

To display the exception message using the toString() method, we can call the toString() method or we can directly use the parameter name.

Example of displaying Exception message using toString() method,

catch(ArithmeticException e) {
     System.out.println(e.toString());
     // Or, 
     System.out.println(e);
 }

getMessage()

The getMessage() method of java.lang.Throwable class returns String. It gives only the reason for the exception. Most of the time we use the getMessage() method to display the message of the exception.

The exception message format, displayed by getMessage():-
<reason for the exception>

Example of displaying Exception message using toString() method,

catch(Exception e) {
    System.out.println(e.getMessage());
}

Message JVM printStackTrace() toString() getMessage()
Thread-name Yes No No No
Exception name Yes Yes Yes No
Reason for
the exception
Yes
Yes Yes Yes
Place of
the exception
Yes Yes No No

Program to demonstrate different ways to get exception message in Java

class DisplayExceptionMsg {

   public static void main(String[] args) {

     try {
       System.out.println(9/0);
     } catch(ArithmeticException e) {

       // getMessage() method
       System.out.println("The getMessage() 
                            method Output: ");
       System.out.println(e.getMessage());
       System.out.println();

       // toString() method
       System.out.println("The toString()
                            method Output: ");
       System.out.println(e.toString());
       // Or, 
       System.out.println(e);
       System.out.println();

       // printStackTrace()
       System.out.println("The printStackTrace() 
                               method Output: ");
       e.printStackTrace();
       System.out.println();

       // JVM default message 
       System.out.println("JVM default 
                          exception message: ");
       throw e;
       /* By using above statement we are
        *  re-throwing the caught exception.
        * 
        * This exception will be caught 
        * by JVM default handler. 
        */
    }
  }
}

Output:-

The getMessage() method Output:
/ by zero

The toString() method Output:
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero

The printStackTrace() method Output:
java.lang.ArithmeticException: / by zero
at com.know.execp.DisplayExceptionMsg.main(DisplayExceptionMsg.java:6)

JVM default exception message:
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.know.execp.DisplayExceptionMsg.main(DisplayExceptionMsg.java:6)


Excercise

From below options find which exception message is displayed by the JVM or by which method of java.lang.Throwable class?

Msg1) java.lang.ArithmeticException: / by zero
at FindExecutionFlow.main(ExceptionPropagation.java:15)

Msg2) Exception in thread “main” java.lang.ArithmeticException: / by zero
at FindExecutionFlow.main(ExceptionPropagation.java:15)

Answer:- The exception message of Msg11 is displayed by the prinStackTrace() and the second exception message is displayed by the JVM. The main difference in the exception message displayed by JVM and printStackTrace() is thread-name. The JVM displayed the name of the thread where the exception occurs but the printStackTrace() method doesn’t display thread-name.


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


Related Java topics

Leave a Reply