Best Practices to Handle Exceptions in Java


Throw Exception

Include descriptive contextual messages in the exception
Throw specific checked exceptions, don’t throw RuntimeException, Exception, or Throwable
  • The checked exception is part of the contract of the method, so client knows what exceptions it can catch and handle.
  • If one method throws both base exception and one of its sub-class exception, we should declare both (the base and the specific sub-class) exception not just the base exception.
Throw exception in the right abstraction
  • Wrap low level exception in custom high level exception
Using Custom Exceptions
Don’t lose original exception
  • It’s common that we re-throw but when we do this, make sure to include the original exception as the cause otherwise we will lose the original error message and stack trace.

Impossible Conditions

Throw early catch late

You want to throw an exception as soon as possible because that makes it easier to find the cause

exceptions are handled at higher levels is because the lower levels don’t know what’s the appropriate course of action to handle the error

Valid parameter, user input early

Logging

Don’t Log the exception and re-throw
  • It will write same exception multiple times.

Catch Exception

  • Catch specific exception, not the Exception class
  • Never swallow the exception
    • If you do not want to handle it, then re-throw it. The top layer must handle the exception and translate it into what the user can understand.
  • Never catch Error or Throwable
Only catch those exceptions that we can handle
  • We may catch it to add more information.
  • But if we can’t handle it, it’s better to let the caller decide what to do.

Use try-catch only if it is difficult to deal with pre-check, such as NumberFormatException.

Finally

Never use return within a finally block.
  • A return statement in a finally block will cause exceptions or result in a discarded return value in the try-catch block.
Don’t throw exception in the finally block
  • It will override the original exception.
Close resources: stream
  • IOUtils.closeQuietly(stream) from Apache commons-io
Using try-with-resources

Be careful of NullPointerException

InterruptedException

  • throw it directly or

Common Exceptions

  • UnsupportedOperationException

Misc

  • Never use exceptions for control flow
Document all exceptions in public API
  • When the exception may throw.
  • How client can handle it.

throw new RuntimeException(“failed to get database”, e);

  throw new IllegalStateException("failed to get database", e);

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)