Errors and Exceptions
Core Python

Errors and Exceptions

Every programmer sees errors daily. Learn to read them calmly, fix the common ones and handle the expected ones.

  1. 1Python Exceptions: try, except, else and finallyAn exception is Python's signal that something went wrong while the program was running. If nothing handles i…
  2. 2Python TypeError: What It Means and How to Fix ItA TypeError means Python was asked to do something with a value whose type doesn't support it, such as adding…Error explained
  3. 3Python KeyError: What It Means and How to Fix ItA KeyError is raised when you use square brackets to ask a dictionary for a key it does not contain, like use…Error explained
  4. 4Python IndexError: list index out of rangeAn IndexError happens when you ask a list, tuple or string for a position that doesn't exist, such as item 3 …Error explained
  5. 5Python NameErrorA NameError means Python looked for a name (a variable, function or module) in every scope it checks and foun…Error explained
  6. 6Python SyntaxErrorA SyntaxError means Python could not parse your code because it breaks the language's grammar, so the whole f…Error explained
  7. 7Python IndentationErrorAn IndentationError means the spaces at the start of a line don't match the block structure Python expected, …Error explained
  8. 8Python ValueErrorA ValueError means a function received a value of the correct type but with content it cannot accept, such as…Error explained
  9. 9Python AttributeErrorAn AttributeError means you used a dot to reach a variable or method that the object does not have, usually b…Error explained
  10. 10Python ModuleNotFoundError: No module namedA ModuleNotFoundError means an import statement named a module that Python could not find in the standard lib…Error explained
  11. 11Python ZeroDivisionError: Division by ZeroA ZeroDivisionError means a division, floor division, modulo or power operation had zero as the divisor, whic…Error explained
  12. 12Python FileNotFoundError: No such file or directoryA FileNotFoundError means Python tried to open or access a file or folder path that does not exist, usually b…Error explained
  13. 13Python UnboundLocalErrorAn UnboundLocalError means a function used a local variable before it had a value, which happens because assi…Error explained
  14. 14Python RecursionErrorA RecursionError means a function kept calling itself (directly or indirectly) until Python's call-stack dept…Error explained
  15. 15Python AssertionErrorAn AssertionError means an assert statement checked a condition that turned out to be False, so something you…Error explained
  16. 16Python StopIterationStopIteration is the exception an iterator raises from next() when it has no more values to give, which for l…Error explained

Start with Python Exceptions: try, except, else and finally →

About Errors and Exceptions

Every programmer sees errors daily, and reading them quickly is a core skill. These lessons explain how to read a traceback, then give one page for each common exception, covering its causes and the fix: TypeError, KeyError, IndexError, NameError, SyntaxError, ValueError and more. Finish with the exceptions lesson on try, except, else, finally and raise to handle failures on purpose.

Related reference: Glossary: exception · Python how-to guides

Further reading (official documentation)

Practice for this topic