← Python Basics

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
  1. What's wrong here: dividing 10?ZeroDivisionError
  2. What's wrong here: int() of a word?ValueError
  3. What's wrong here: adding to the list?AttributeError
  4. What's wrong here: the string?SyntaxError
  5. What does the except block print?oops
  6. What does the finally block print?done
  7. What does the else branch print?6
  8. What does printing the caught error show?bad age
  9. Fill the blank to catch the errorexcept
  10. Keyword that triggers an error on purposeraise
  11. 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