Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Examples | Error Handling
course content

Course Content

Mastering Python: Annotations, Errors and Environment

ExamplesExamples

Let's consider the most useful exceptions in Python.

TypeError

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.

ValueError

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.

ZeroDivisionError

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

SyntaxError

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

Note

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
course content

Course Content

Mastering Python: Annotations, Errors and Environment

ExamplesExamples

Let's consider the most useful exceptions in Python.

TypeError

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.

ValueError

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.

ZeroDivisionError

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

SyntaxError

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

Note

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
some-alt