Alfred James
Alfred James

Reputation: 1019

PrintStackTrace() in Exception Handling

What does this method do? I found an Android code on internet about exception handling in which the guy used this function in catch{} He didn't explain properly why this function/method is used.

Upvotes: 0

Views: 531

Answers (4)

yohlulz
yohlulz

Reputation: 133

Check this JavaDoc. Stacktrace is used in debug mode. Each method is displayed from most generat to the specific method where the exception was thrown.

Think about it as a stack which has each method call involved in the cause of that exception.

In addition, using printStackTrace() method is considered bad practice because System.err file is used and the method is not thread safe. Better use a logger.

Upvotes: 1

Chris Ballance
Chris Ballance

Reputation: 34347

It prints the stack trace of the Exception to System.err.

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

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

Upvotes: 0

Varun
Varun

Reputation: 1004

This method prints the entire stack trace of the methods that got called before the exception has been raised. It is very helpful while debugging since you will know the path of execution.

Upvotes: 0

Rocky Pulley
Rocky Pulley

Reputation: 23301

it prints the stack where the exception occured. If there is debug information then it will show the line of code where each call was at to help you find the source of the problem.

Upvotes: 0

Related Questions