Chapter 7
Errors and exceptions
Errors are how Python tells you what went wrong, and each has a name: ZeroDivisionError, ValueError, AttributeError, SyntaxError and more. This chapter teaches you to read them, and to handle them with try and except, so a program can recover instead of crashing. It covers else and finally, raising your own errors with a message, and where to look first in a traceback: its last line.
11 cards
See the 11 cards
- What's wrong here: dividing 10?ZeroDivisionError
- What's wrong here: int() of a word?ValueError
- What's wrong here: adding to the list?AttributeError
- What's wrong here: the string?SyntaxError
- What does the except block print?oops
- What does the finally block print?done
- What does the else branch print?6
- What does printing the caught error show?bad age
- Fill the blank to catch the errorexcept
- Keyword that triggers an error on purposeraise
- The last line of a traceback tells you…The error type and message
Image credits
- What's wrong here: dividing 10?: Brain Zap, CC0 1.0 Source
- What's wrong here: int() of a word?: Brain Zap, CC0 1.0 Source
- What's wrong here: adding to the list?: Brain Zap, CC0 1.0 Source
- What's wrong here: the string?: Brain Zap, CC0 1.0 Source
- What does the except block print?: Brain Zap, CC0 1.0 Source
- What does the finally block print?: Brain Zap, CC0 1.0 Source
- What does the else branch print?: Brain Zap, CC0 1.0 Source
- What does printing the caught error show?: Brain Zap, CC0 1.0 Source
- Fill the blank to catch the error: Brain Zap, CC0 1.0 Source