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

后端 未结 10 1770
不知归路
不知归路 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条回答
  • 2020-12-02 07:25

    It helps to trace the exception. For example you are writing some methods in your program and one of your methods causes bug. Then printstack will help you to identify which method causes the bug. Stack will help like this:

    First your main method will be called and inserted to stack, then the second method will be called and inserted to the stack in LIFO order and if any error occurs somewhere inside any method then this stack will help to identify that method.

    0 讨论(0)
  • 2020-12-02 07:25

    printStackTrace() prints the locations where the exception occurred in the source code, thus allowing the author who wrote the program to see what went wrong. But since it shows problems in the source code, the user(s) who may or may not have any coding experience may not be able to understand what went wrong, so if the program allows the user to send error messages to the authors, the users may not be able to give good data on what went wrong.

    You should consider the Logger.getLogger() method, it offers a better exception handling (logging) facility, and besides printStackTrace() without arguments is considered to be obsolete and should ONLY be used for debugging purposes, not for user display.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-02 07:28

    printStackTrace() helps the programmer to understand where the actual problem occurred. It helps to trace the exception. 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.

    The following is an another example of print stack of the Exception in Java.

    public class Demo {
       public static void main(String[] args) {
          try {
             ExceptionFunc();
          } catch(Throwable e) {
             e.printStackTrace();
          }
       }
       public static void ExceptionFunc() throws Throwable {
          Throwable t = new Throwable("This is new Exception in Java...");
    
          StackTraceElement[] trace = new StackTraceElement[] {
             new StackTraceElement("ClassName","methodName","fileName",5)
          };
          t.setStackTrace(trace);
          throw t;
       }
    }
    

    java.lang.Throwable: This is new Exception in Java... at ClassName.methodName(fileName:5)

    0 讨论(0)
提交回复
热议问题