Avoid printStackTrace(); use a logger call instead

前端 未结 7 1256
我寻月下人不归
我寻月下人不归 2020-12-07 11:05

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

  • Avoid printStackTrace(); use a logger call instead.
7条回答
  •  一个人的身影
    2020-12-07 11:31

    It means you should use logging framework like logback or log4j 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.

提交回复
热议问题