It might seem odd to have a section about error handling in a book about clean code. Error
handling is just one of those things that we all have to do when we program. Input can be
abnormal and devices can fail. In short, things can go wrong, and when they do, we as programmers
are responsible for making sure that our code does what it needs to do.
The connection to clean code, however, should be clear. Many code bases are completely
dominated by error handling. When I say dominated, I don’t mean that error handling
is all that they do. I mean that it is nearly impossible to see what the code does
because of all of the scattered error handling. Error handling is important, but if it
obscures logic, it’s wrong.
In this chapter I’ll outline a number of techniques and considerations that you can use
to write code that is both clean and robust—code that handles errors with grace and style.