Exception Handling
Exception Handling Best Practices
Review essential strategies for catching specific types, avoiding swallow traps, and structuring clean error layers.
Interview: Evaluates production-level design patterns, avoiding anti-patterns like log-and-throw or swallowing, and exception strategy trade-offs.
Robust exception handling is essential for maintaining application stability and auditability. Following standard best practices prevents resource leaks, avoids duplicated logging, and keeps error layers clean.
Never Swallow
Swallowing exceptions hides failures. Always log or wrap exceptions to keep failures visible.
Log OR Throw
Avoid logging and then throwing the same exception. This duplicates logs, cluttering diagnostic outputs.
Boundary Wrappers
Wrap low-level technology exceptions in clean domain exceptions before passing them across system layers.
The Log-and-Throw Anti-Pattern
A common mistake in Java development is logging an exception and then throwing it again:
// Anti-pattern: Log and Throw
catch (IOException e) {
logger.error("Failed to read file", e);
throw e;
}
This causes the exception to be logged multiple times as it propagates up the call stack, cluttering the log files and complicating debugging. The rule of thumb is: Log OR Throw. If you throw, let the calling method log it.
Common Pitfalls
- Swallowing Exceptions: Leaving catch blocks empty, which hides runtime bugs.
- Using printStackTrace() in Production: Using
e.printStackTrace()instead of a logger. This writes to standard output, which is not redirected to log files and can cause thread blocking.
Best Practices
- Catch Specific Exception Types: Avoid catch-all
catch (Exception e)blocks when specific exceptions can be handled individually. - Use Try-With-Resources: Always manage resource cleanup using try-with-resources to prevent memory and file descriptor leaks.
Interview-Relevant Information
Q1: Why is log-and-throw considered an anti-pattern?
Answer: It causes duplicate logs. When an exception is logged and then re-thrown, every caller class up the stack typically logs it again, cluttering the log files and complicating diagnostics.
Q2: What is the risk of utilizing printStackTrace() in production web applications?
Answer: printStackTrace() writes to standard error, which often bypasses log configuration systems and can block executing threads under heavy load. It also leaks internal class structures to the console, raising security concerns.
Quick Checklist
Can you explain the log-and-throw anti-pattern, describe why printStackTrace is avoided in production, and list three rules of clean exception design? If yes, you understand exception handling best practices.
Use Cases
Building global exception handler filters inside Spring Boot applications.
Designing clean transactional layers in enterprise business applications.
Common Mistakes
Swallowing exceptions inside empty catch blocks, hiding runtime bugs.
Logging exceptions at every layer before throwing, cluttering log files.