Donnerstag, 24. Mai 2012

Why using e.printStackTrace() is a bad idea

This is my personal opinion, so feel free to leace a comment if you think differnt.

e.printStackTrace() is by default added to every catch block automatically generated by eclipse and so often stays in the code forever. Even in libraries given to the world you can find it, and the worst, not change it anymore.

In my actual case I wrote a program to run some textmining process. I introduced an exception handling which propagates all exceptions up to my two main functions, where they are cought and logged. The type of the exception as well as the error message are logged in the SEVERE level and the stack trace in the FINE level. My code is using the UIMA framework, which is a nice framework to process unstructured information, but somehow one of the developer left a e.printStackTrace() in. In general they use a modified Java logger, so I do not know why they have left e.printStackTrace() in, but it really is annoying for me. Due to this one line of code, I get every exception from the deep of the UIMA framework printed at least twice in my console. And I cannot do anything about it. The only solution would be to log into a file, which is perhaps usable for big runs, but not during development, and this is where this additional prints annoy me and my colleagues most.

So my advice: Do not use e.printStackTrace() anywhere. Just use the build in logger (or if you use an older Java version log4j) and give other coders the possibiliy to influence the outout behaviour of your code.
This counts double if you are writing a lib!

Keine Kommentare:

Kommentar veröffentlichen