March 10, 2009

5 Tips for Error Handling in Java


  1. Avoid throwing an exception whenever possible. In stead return special values e.g. null.

  2. Throw early, catch later

  3. Either throw an exception or log them but never do both

  4. Always throw a checked exception, unless it’s a severe error that cannot be handled by the caller of the api.

  5. Maintain layer wise clear abstraction on errors and exceptions so that even if there is a change in the lower layer, consuming layers should not be affected. A particular exception from one layer should never be carried onto the next layer.

No comments: