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

后端 未结 10 1769
不知归路
不知归路 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:05

    See image here

    printStackTrace() tells at what line you are getting error any why are you getting error.

    Example:

     java.lang.ArithmeticException: / by zero
        at MinNumber.main(MinNumber.java:8) 
    
    0 讨论(0)
  • 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());  
             }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 07:12

    The printStackTrace() helps the programmer understand where the actual problem occurred. The printStackTrace() method is a member of the class Throwable in the java.lang package.

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

    It's a method on Exception instances that prints the stack trace of the instance to System.err.

    It's a very simple, but very useful tool for diagnosing an exceptions. It tells you what happened and where in the code this happened.

    Here's an example of how it might be used in practice:

    try {
        // ...
    } catch (SomeException e) { 
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-12-02 07:19

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

    Well, the purpose of using this method e.printStackTrace(); is to see what exactly wrong is.

    For example, we want to handle an exception. Let's have a look at the following Example.

    public class Main{
    
      public static void main(String[] args) {
    
        int a = 12;
        int b = 2;
    
        try {
           int result = a / (b - 2);
           System.out.println(result);
        }
    
        catch (Exception e)
        {
           System.out.println("Error: " + e.getMessage());
           e.printStackTrace();
        }
      }
    }
    

    I've used method e.printStackTrace(); in order to show exactly what is wrong.

    In the output, we can see the following result.

    Error: / by zero
    
    java.lang.ArithmeticException: / by zero
    
      at Main.main(Main.java:10)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
    
    0 讨论(0)
  • 2020-12-02 07:24

    printStackTrace() helps the programmer to understand where the actual problem occurred. printStacktrace() is a method of the class Throwable of java.lang package. It prints several lines in the output console. The first line consists of several strings. It contains the name of the Throwable sub-class & the package information. From second line onwards, it describes the error position/line number beginning with at.

    The last line always describes the destination affected by the error/exception. The second last line informs us about the next line in the stack where the control goes after getting transfer from the line number described in the last line. The errors/exceptions represents the output in the form a stack, which were fed into the stack by fillInStackTrace() method of Throwable class, which itself fills in the program control transfer details into the execution stack. The lines starting with at, are nothing but the values of the execution stack. In this way the programmer can understand where in code the actual problem is.

    Along with the printStackTrace() method, it's a good idea to use e.getmessage().

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