Avoid printStackTrace(); use a logger call instead

user1305398 picture user1305398 · May 7, 2012 · Viewed 103.3k times · Source

In my application, I am running my code through PMD.It shows me this message:

  • Avoid printStackTrace(); use a logger call instead.

What does that mean?

Answer

Tomasz Nurkiewicz picture Tomasz Nurkiewicz · May 7, 2012

It means you should use logging framework like or and instead of printing exceptions directly:

e.printStackTrace();

you should log them using this frameworks' API:

log.error("Ops!", e);

Logging frameworks give you a lot of flexibility, e.g. you can choose whether you want to log to console or file - or maybe skip some messages if you find them no longer relevant in some environment.