Chapter 5. Errors, Logging, and Debugging

In this chapter, I’ll introduce some techniques for making your code more robust. Robustness is one of the principles of writing good code that I discussed in Chapter 1. First, I’ll discuss how to handle errors in your code so that your code behaves predictably even if something goes wrong. Next, I’ll show you how to save information about what your code is doing by logging it, which will help other people reason about your code and also help when an unexpected error occurs. Finally, I’ll talk about debugging, which is how to track down sources of problems in your code. I’ll explain some strategies and tools for efficient debugging.

Errors in Python

An error is when your code stops unexpectedly before the program has completed all the tasks it is supposed to do. If this happens, whatever depends on your code may also stop. Sometimes, this is what you want to happen, but sometimes you want something else to happen so that your code continues running. This is known as handling the error. Your code should be predictable for the set of things that you expect to happen, and this makes it robust.

In this section I’ll discuss how to read Python error messages, how to handle them, and how to raise your own errors.

Reading Python Error Messages

Python error messages may look cryptic, but they are full of useful information. There are two types: syntax errors and exceptions. Syntax errors arise when you write code that isn’t completely correct ...

Get Software Engineering for Data Scientists now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.