getMessage() printStackTrace() Java

Catch block is meant to give a message to the user when an exception occurs during the execution of a program. There are different styles used by the programmer where each style gives different messages. Programmer can choose one on his needs. Few. styles are using getMessage() printStackTrace()

1st Style

Following is the general style used by the programmer.

public class Demo
{
  public static void main(String args[])
  {
    try
    {
      System.out.println(10/0);
    }
    catch(ArithmeticException e)
    {
      System.out.println("Do no divide zero by sir. " + e);
    }
   }
}

getMessage() printStackTrace()

       System.out.println("Do no divide zero by sir. " + e);

The advantage of this style is the actual user gets both the user defined massage ("Do no divide zero by sir") and system defined message (given by object e). See the above screenshot.

2nd Style

       catch(ArithmeticException e)
       {
          System.out.println("Do no divide zero by sir.");
       }

If the programmer would not like the user not to confuse or bother much about system defined message, he may write as in the above catch block.

3rd Style

        catch(ArithmeticException e)
        {
           System.out.println(e);
        }

The above catch block prints only system defined message. This style can be used by the programmer at code developing phase but not to the user as actual user, most often, is not a computer background person like bank employee or a business person.

4th Style

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

getMessage() printStackTrace()

getMessage() is a method of Throwable class (super class of all exceptions of Java) inherited by every exception class like ArithmeticException as in the above code. The getMessage() method prints only the message part of the output printed by object e. This style can be preferred when the programmer would not like to give his message to the actual user.

Following is the method signature

public java.lang.String getMessage();

The method returns a string value, the message.

5th Style

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

getMessage() printStackTrace()

Observe, it is printStackTrace() method of Throwable class inherited by every exception class. This method prints the same message of e object and also the line number where the exception occurred. This is most used by the programmer in sample code development and very much useful to debug the program. One more advantage is, it avoids typing of System.out.println() as printStackTrace() does not return any value (returns void).

Following is the method signature

public void printStackTrace();

The above method takes the exception object from the stack, converts internally to a string and prints.

Some more discussion of printing exception messages is available at Creating User-defined Exceptions.

View All for Java Differences on 90 Topics

2 thoughts on “getMessage() printStackTrace() Java”

Leave a Comment

Your email address will not be published.