Notes on Clean Code — Ch.7 Error Handling
This is my note on Clean Code by Robert C. Martin.
This chapter is all about error handling. In my personal projects, I often ignore importance of error handling, so this is definitely important chapter for me.
Use Exceptions Rather Than Return Codes
It is much cleaner code when the error is handled by exceptions rather than returned values. Using conditions on the returned value to handle error becomes difficult to read and easy to forget to check errors.
Write Try-Catch-Finally Statement First
It is a good practice to start with try-catch statement when you are writing code that could throw exceptions. This helps you define what the user of that code should expect, no matter what goes wrong with the code that is executed in the try
Provide Context with Exceptions
Don’t rely on stack trace. Write intent of the exceptions. Mention the operation that failed and the type of failure.
Wrap 3rd Party API with Your Own Exceptions
When you wrap 3rd party API, you minimize your dependencies upon it. You can choose to move to a different library in the future without much penalty.
Avoid Return and Pass Null
A lot of checks need to be made if handling Null.