12.6. Summary

One of the most important things that you as a developer can do to reduce the cost of owning your software for your customers is to improve your error-handling code. If your application encounters errors, either those errors need to be handled correctly or the application needs to fail rather than enter an inconsistent state.

Rather than writing pervasive error-handling code that checks return values, use exceptions if your coding environment or language supports them. Using exceptions properly can greatly reduce the amount of error-handling code you need to write, as well as ensure that errors are reported to the caller and that the application exits rather than proceeding in a bad state.

To make best use of error handling in your code, establish a clear and simple policy for handling errors, and make sure that every member of your team is familiar with that policy. Keep it short and easy to read, and make sure that you follow up by reviewing the code your team produces to ensure that the policy is understood properly and is being followed consistently. Following a well-crafted policy consistently will make your software easier for both developers and customers to use and support.

Handle errors as close to the code that causes those errors as possible to avoid getting into a bad state, but make sure that you are handling only those errors that you can recover from.

Combined with a good tracing/error-reporting policy, good error handling will make it easier for users ...

Get Code Leader: Using People, Tools, and Processes to Build Successful Software now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.