Sign in
Log inSign up
"Clean Code" book summary, Part 6 : Error handling.

"Clean Code" book summary, Part 6 : Error handling.

Amir Anwar's photo
Amir Anwar
·Mar 7, 2017

Use the following simple and straight forward rules to help making the error handling code cleaner:

  • There must be a good amount of separation between your business logic code and your error handling code.
  • Too much error handling is not good, don't use If statements instead of try and catch blocks.
  • Try to provide context with exceptions, i.e. Create informative error messages and pass them along with your exceptions. Mention the operation that failed and the type of failure.
  • If you are using logging in your application, pass along enough information to be able to log the error in your catch.
  • If we have a method that in some cases will return a value/object that might cause an error, usually we will surround the output from this method with try and catch, but, as a better practice, we should refactor this method to always return an error-free value/object.
  • Don't Return null, Don't pass null to methods

To be continued...

Start the summary from here