Exception Handling
About
Importance of Exception Handling in Code Style
General Best Practices
1. Use Specific Exception Types
// Good
throw new InvalidAccountStateException("Account is already closed.");
// Avoid
throw new RuntimeException("Something went wrong.");2. Meaningful Exception Naming
3. Avoid Empty Catch Blocks
4. Use try-with-resources for Closables
try-with-resources for Closables5. Avoid Catching Exception or Throwable Broadly
Exception or Throwable Broadly6. Always Log with Context
7. Use Exception Chaining (cause)
cause)8. Avoid Logic in Catch Blocks
9. Do Not Use Exceptions for Flow Control
10. Centralize Exception Translation
Exception Class Code Style Guidelines
Last updated