Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions content/python/concepts/errors/errors.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Errors'
Description: 'There are (at least) two distinguishable kinds of errors in Python: syntax errors and exceptions. Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python: error File "script.py", line 1 while True print("Hello world!") ^ SyntaxError: invalid syntax'
Description: 'The two types of errors in Python are syntax errors and exceptions. Exceptions may arise even if the code is syntactically correct.'
Subjects:
- 'Computer Science'
- 'Data Science'
Expand All @@ -12,13 +12,13 @@ CatalogContent:
- 'paths/computer-science'
---

There are (at least) two distinguishable kinds of errors in Python: syntax errors and exceptions.
The two types of **errors** in Python are syntax errors and exceptions.

## Syntax Errors

Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python:
Syntax errors (also known as parsing errors) occur when a sequence of characters, or tokens, violates the syntax of the Python programming language:

```error
```shell
File "script.py", line 1
while True print("Hello world!")
^
Expand All @@ -27,19 +27,19 @@ SyntaxError: invalid syntax

The parser repeats the offending line and displays a little arrow `^` pointing at the earliest point in the line where the error was detected.

The error is caused by (or at least detected at) the token preceding the arrow in the example, the error is detected at the function `print()`, since a colon `:` is missing before it.
The error is caused by (or at least detected at) the token preceding the arrow in the example, the error is detected at the [`print()`](https://www.codecademy.com/resources/docs/python/built-in-functions/print) function, since a colon `:` is missing before it.

File name and line number are printed so you know where to look in case the input came from a script.
File name and line number are printed to state where the error originated.

## Exceptions

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal. Most exceptions are not handled by programs, however, and result in error messages as shown here:

### Value Error

`ValueError` is thrown when a function's argument is of an inappropriate type.
`ValueError` is thrown when a function's argument is of the correct type, but an inapprpriate value, such as being out of range.

```error
```shell
Traceback (most recent call last):
File "script.py", line 1, in <module>
int('xyz')
Expand All @@ -50,7 +50,7 @@ ValueError: invalid literal for int() with base 10: 'xyz'

`NameError` is thrown when an object could not be found.

```error
```shell
Traceback (most recent call last):
File "script.py", line 1, in <module>
age
Expand All @@ -61,7 +61,7 @@ NameError: name 'age' is not defined

`IndexError` is thrown when trying to access an item at an invalid index.

```error
```shell
Traceback (most recent call last):
File "script.py", line 1, in <module>
employees[3]
Expand All @@ -72,18 +72,29 @@ IndexError: list index out of range

`ModuleNotFoundError` is thrown when a module could not be found.

```error
```shell
Traceback (most recent call last):
File "script.py", line 1, in <module>
import notamodule
ModuleNotFoundError: No module named 'notamodule'
```

### Type Error

`TypeError` is thrown when a function's argument is of an inappropriate type.

```shell
Traceback (most recent call last):
File "script.py", line 1, in <module>
max(True)
TypeError: 'bool' object is not iterable
```

### Zero Division Error

`ZeroDivisionError` is thrown when the second operator in the division is zero.

```error
```shell
Traceback (most recent call last):
File "script.py", line 1, in <module>
ratio = 100 / 0
Expand Down