I have used e.printStackTrace in one of my Java applications. However, I read in some forum that we should avoid using printStackTrace(). Could somebody explain to me why I should avoid using e.printStackTrace() and what are the alternatives to it?
Please share the piece of code for an alternative to e.printStackTrace()
Alternative to e.printStackTrace() and an example code will be helpful
6k Views Asked by Anjit Singha At
3
There are 3 best solutions below
0
On
Loggers should be used instead of printing the whole stack trace on stream. e.printStackTrace() prints a Throwable and its stack trace to stream which could inadvertently expose sensitive information.
Loggers should be used instead to print Throwables, as they have many advantages.
more info : https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure.html
You should avoid it because it is better to use a logger like Log4j, so that you can write logs in a file & manage the logging better.
Indeed, the method
is used to print the stack of the exception directly in the console, and that (for a production application) is bad.