Let's consider the most useful exceptions in Python.


This exception is raised when a function takes the value with an unexpected type.

The len() function returns the length of the received data. The int type hasn't length (the len() function doesn't work with the int data type), so the interpreter raises the TypeError.


This exception is raised when a function or method takes an unexpected value.

The imported sqrt() function returns the square root of a number. In the example above, the sqrt() function received -5 (negative number), but the square root cannot be taken from a negative number, so the interpreter raises the ValueError.


It is a well-known fact: you cannot divide by zero. The interpreter also understands this and therefore raises a ZeroDivisionError:


This is an exception of a completely different nature. If previous exceptions were raised by the interpreter, the SyntaxError raises by the linter.


The linter is a tool that analyses your code before executing.

If other errors are raised during program execution, SyntaxError is raised before execution.

Look at the example:

Pay attention that the two first prints are not executed. In the previous exception cases (TypeError, ValueError, and ZeroDivisionError), all code before the exception was executed.

Everything was clear?

Section 3. Chapter 2