If I use a logger in the case of known exceptions, what’s wrong with e.printStackTrace() for an unknown exception ?
I’m always told not to do this – but not given a reason
example below
try {
dostuff();
} catch (AException ae) {
logger.error("ae happened");
} catch (BException be) {
logger.error("be happened");
} catch (CException ce) {
logger.error("ce happened");
} catch (Exception e) {
e.printStackTrace();
}
Because it doesn’t use the logger system, it goes directly to the
stderrwhich has to be avoided.edit: why writing directly to stderr has to be avoided ?
In answer to your question, @shinynewbike, I have slightly modifed my answer. What it has to be avoided is to write directly to
stderrwithout using aloggercapability.loggersprovide useful features to change logging traces by priority and packages, among other things they also allow to redirect traces to different output mechanisms … queues, files, databases, streams …When you write directly to
System.errorSystem.outthen you lose these features, and what is worse if you mixloggerandSystem.err.writeyou might end up getting traces in different ‘files’ which will make debugging your system difficult.