CHAPTER 10
Handling Errors
In this chapter, you learn how to handle errors in Python. First, we quickly review the different types of errors that occur in computer code and the ways you can catch the different types. We then focus on using try… except
blocks to handle errors in your Python code. You learn to cause errors, trap exceptions, and create custom exceptions.
Understanding the Various Types of Errors
Cause Errors and Trap Exceptions
Add an else
Block or a finally
Block
Understanding the Various Types of Errors
In this section, you learn about the different types of errors that can occur in code, what causes them, and what you can do to eliminate them.
We start with compile-time errors, errors that occur when Python is unable to create workable instructions from the commands in a script. Many compile-time errors are syntax errors, mistakes in the structure of the code. We then move on to runtime errors, errors that occur after Python has compiled a script successfully and has moved on to executing it. Runtime errors include semantic errors and logical errors.
Compile-Time Errors
Python is generally considered an interpreted language, which is often understood to mean that it is not a compiled language. But the difference between ...
Get Teach Yourself VISUALLY Python 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.