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

I am viewing a socket program. In it, printStackTrace is called in the IOException object in the catch block.
What does printStackTrace() actually do?

 catch(IOException ioe) { ioe.printStackTrace(); } 

I do not know his purpose. What is it used for?

+67
java exception-handling
Apr 01 '10 at
source share
8 answers

It prints an Exception stack trace to System.err .

This is a very simple but very useful tool for diagnosing exceptions. He tells you what happened and where in the code it happened.

+75
Apr 01 2018-10-12T00:
source share

I was also curious about this, so I just put together a small code example where you can see what it does:

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

println(e):

java.lang.NullPointerException

e.printStackTrace:

 java.io.IOException at package.Test.main(Test.java:74) 
+31
Feb 14 '14 at 19:43
source share

It helps track the exception. For example, you write several methods in your program, and one of your methods causes an error. Printstack then helps you determine which method is causing the error. The stack will help something like this:

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

+13
Apr 23 2018-12-12T00:
source share

printStackTrace () helps the programmer understand where the real problem occurred. printStacktrace () is a method of the Throwable class of the java.lang package . It prints a few lines in the output console. The first line consists of several lines. It contains the Throwable subclass name and package information. From the second line, it describes the error number / line number starting with " on ".

The last line always indicates the destination affected by the error / exception. The second last line tells us the next line in the stack, where the control goes after receiving the translation from the line number indicated on the last line. Errors / exceptions represent the output in the form of a stack, which was loaded onto the stack by the fillInStackTrace () method of the Throwable class, which itself fills the transfer of control of the part program to the execution stack. Lines beginning with in are nothing but the values โ€‹โ€‹of the execution stack. Thus, the programmer can understand where the actual problem is in the code.

Along with the printStackTrace () method, it is recommended to use e.getmessage () .

+9
Aug 01 '15 at 16:42
source share

printStackTrace (): prints the places where an exception occurred in the source code, which allows 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, therefore, if the program allows the user to send error messages to the authors, Users may not be able to give good data on what went wrong.

You should consider the Logger.getLogger () method, it offers a better way to handle logging, and moreover, printStackTrace () with no arguments is considered obsolete and should ONLY be used for debugging purposes, not a display for the user.

+4
May 28 '15 at 3:10
source share

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

+2
Jun 30 '17 at 18:25
source share

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

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

For example, we want to handle an exception. Let's 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 used the e.printStackTrace(); method e.printStackTrace(); to show exactly what is wrong.

At 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
Feb 26 '17 at 0:16
source share

printStackTrace is a method of the Throwable class. This method displays an error message on the console, where we get an exception in the source code. These methods can be used with a catch block and describe:

  • The name of the exception.
  • Description of exception.
  • Where do we get the exception.

There are three methods that describe an exception on the console in which printStackTrace is one of them. these three methods:

  • printStackTrace()
  • toString()
  • 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
Jul 12 '17 at 20:47
source share



All Articles