[java] What is the use of printStackTrace() method in Java?

printStackTrace is a method of the Throwable class. This method displays error message in the console; where we are getting the exception in the source code. These methods can be used with catch block and they describe:

  1. Name of the exception.
  2. Description of the exception.
  3. Location of the exception in the source code.

The three methods which describe the exception on the console (in which printStackTrace is one of them) are:

  1. printStackTrace()
  2. toString()
  3. getMessage()

Example:

public class BabluGope {
    public static void main(String[] args) {
        try {
            System.out.println(10/0);
         } catch (ArithmeticException e) {
             e.printStackTrace();   
             // System.err.println(e.toString());
             //System.err.println(e.getMessage());  
         }
    }
}