I am going through a socket program. In it, printStackTrace
is called on the IOException
object in the catch block.
What does printStackTrace()
actually do?
catch(IOException ioe)
{
ioe.printStackTrace();
}
I am unaware of its purpose. What is it used for?
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 besidesprintStackTrace()
without arguments is considered to be obsolete and should ONLY be used for debugging purposes, not for user display.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.
java.lang.Throwable: This is new Exception in Java... at ClassName.methodName(fileName:5)
printStackTrace()
helps the programmer to understand where the actual problem occurred.printStacktrace()
is a method of the classThrowable
ofjava.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 withat
.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 ofThrowable
class, which itself fills in the program control transfer details into the execution stack. The lines starting withat
, 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 usee.getmessage()
.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.
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.
I've used method
e.printStackTrace();
in order to show exactly what is wrong.In the output, we can see the following result.
The
printStackTrace()
helps the programmer understand where the actual problem occurred. TheprintStackTrace()
method is a member of the classThrowable
in thejava.lang
package.