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

后端 未结 10 1843
不知归路
不知归路 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
    慢半拍i (楼主)
    2020-12-02 07:26

    I was kind of curious about this too, so I just put together a little sample code where you can see what it is doing:

    try {
        throw new NullPointerException();
    }
    catch (NullPointerException e) {
        System.out.println(e);
    }
    
    try {
        throw new IOException();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    System.exit(0);
    

    Calling println(e):

    java.lang.NullPointerException

    Calling e.printStackTrace():

    java.io.IOException
          at package.Test.main(Test.java:74)
    

提交回复
热议问题