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

后端 未结 10 1822
不知归路
不知归路 2020-12-02 06:35

I am going through a socket program. In it, printStackTrace is called on the IOException object in the catch block.
What does printStackT

10条回答
  •  萌比男神i
    2020-12-02 07:09

    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());  
             }
        }
    }
    

提交回复
热议问题