What are the differences between printStackTrace() method and getMessage() method in Java?



The errors that occur during runtime after compilation are called exceptions. To handle and rectify these errors, it is necessary to understand the cause and the point where the error occurs.

There are two ways to find the details of the exception: one is the printStackTrace() method, and another is the getMessage() method. Both are used for exception handling in Java, but they are different from each other. Let's discuss how.

The printStackTrace() Method

The printStackTrace() method is defined in Throwable class of java.lang package and it is inherited into both java.lang.Error class and java.lang.Exception class. When you use it in your code, it will display the name of the exception, and nature of the message, and the line number where that exception has occurred.

Syntax

public void printStackTrace()

Example

In this Java program, we are using the printStackTrace() method in the catch block to get the description of the exception in case of any runtime error.

public class PrintStackTraceMethod {
   public static void main(String[] args) {
      try {
         int a[]= new int[5];
         a[5]=20;
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
} 

Since the array defined in the above code has an index range from 0 to 4, it will produce an ArrayIndexOutOfBoundsException. The details of the exception are as:

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at PrintStackTraceMethod.main(PrintStackTraceMethod.java:5) 

The getMessage() method

The getMessage() method is defined in java.lang.Throwable class and it is inherited from java.lang.Error and java.lang.Exception classes. This method will display the only exception message.

Syntax

public String getMessage()

Example

In the following Java code, we are using the getMessage() method to get the error message in case of any runtime error.

public class GetMessageMethod {
   public static void main(String[] args) {
      try {
         int x=1/0;
      } catch (Exception e) {
         System.out.println(e.getMessage());
      }
   }
} 

There is an error in the above code, which will be displayed by the getMessage() method:

/ by zero 

The printStackTrace() VS getMessage() Method

The following table shows the difference between the printStackTrace() and getMessage() methods in Java:

printStackTrace()

getMessage()

The printStackTrace() method displays full exception details. The details include type, line number, and call stack.

The getMessage() method displays only a simple exception message. There will be no description.

It does not return anything as its return type is void.

It returns a message of string type.

You can use this method during development and debugging.

You can use this method during production as it prints a user-friendly message.

Updated on: 2025-05-19T19:36:14+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements