No Throwable.printStackTrace()
ID |
java.no_throwable_printstacktrace |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Efficiency |
Language |
Java |
Tags |
best-practice |
Rationale
printStackTrace() writes the stack trace to System.err, which is not a proper logging destination. In production applications the output may be lost, interleaved with other output, or unavailable to monitoring systems. It also bypasses log-level filtering and structured logging.
// Bad - stack trace goes to stderr
try {
riskyOperation();
} catch (Exception e) {
e.printStackTrace();
}
Remediation
Use a logging framework (e.g., SLF4J, Log4j, java.util.logging) to log exceptions with the appropriate severity level.
// Good - use a logger
try {
riskyOperation();
} catch (Exception e) {
logger.error("Operation failed", e);
}