Errors

Preview: @timm167 1 total contribution
timm167's avatar
timm167's avatar
@timm167

1 total contribution

Published Jun 4, 2021Updated Jan 12, 2025
Contribute to Docs

The two types of errors in Python are syntax errors and exceptions.

Syntax Errors

Syntax errors (also known as parsing errors) occur when a sequence of characters, or tokens, violates the syntax of the Python programming language:

File "script.py", line 1
while True print("Hello world!")
^
SyntaxError: invalid syntax
Copy to clipboard
Copy to clipboard

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 print() function, since a colon : is missing before it.

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 the correct type, but an inappropriate value, such as being out of range.

Traceback (most recent call last):
File "script.py", line 1, in <module>
int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'
Copy to clipboard
Copy to clipboard

Name Error

NameError is thrown when an object could not be found.

Traceback (most recent call last):
File "script.py", line 1, in <module>
age
NameError: name 'age' is not defined
Copy to clipboard
Copy to clipboard

Index Error

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

Traceback (most recent call last):
File "script.py", line 1, in <module>
employees[3]
IndexError: list index out of range
Copy to clipboard
Copy to clipboard

Module Not Found Error

ModuleNotFoundError is thrown when a module could not be found.

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

Type Error

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

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

Zero Division Error

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

Traceback (most recent call last):
File "script.py", line 1, in <module>
ratio = 100 / 0
ZeroDivisionError: division by zero
Copy to clipboard
Copy to clipboard

Custom Exceptions

Custom exceptions can be used to define and handle application-specific errors. This can be done by creating a sub-class of the built-in Exception class.

Custom exceptions help provide clear feedback to users about unexpected behavior, aid in debugging, and can be used throughout the program.

Defining Custom Exceptions

To define a custom exception, a new class can be created by inheriting Exception and the constructor can be optionally overridden to provide additional information:

# Custom exception for invalid age input
class InvalidAgeError(Exception):
  def __init__(self, message, age=None):
    super().__init__(message)
    self.age = age

Using Custom Exceptions

Once a custom exception has been created, it can raise custom error messages for invalid inputs. For example, the following function raises an error if the user inputs an age not between 0 and 130:

def validate_age(age):
  if not isinstance(age, int):
    raise InvalidAgeError("Age must be an integer.", age)
  if age < 0 or age > 130:
    raise InvalidAgeError(f"{age} is not a valid age. Please choose an age between 0 and 130.", age)

Example

Once the custom exception is defined, it can be used to handle errors in a try-except block. The following example demonstrates how to raise the custom exception when an invalid age is provided:

class InvalidAgeError(Exception):
def __init__(self, message, age=None):
super().__init__(message)
self.age = age
def validate_age(age):
if not isinstance(age, int):
raise InvalidAgeError("Age must be an integer.", age)
if age < 0 or age > 130:
raise InvalidAgeError(f"{age} is not a valid age. Please choose an age between 0 and 130.", age)
try:
validate_age(-5)
except InvalidAgeError as e:
print(f"Error: {e}")
Copy to clipboard
Copy to clipboard

Here is the output:

Error: -5 is not a valid age. Please choose an age between 0 and 130.
Copy to clipboard
Copy to clipboard

All contributors

  1. Preview: @timm167 1 total contribution
    timm167's avatar
    timm167's avatar
    @timm167

    1 total contribution

  2. Preview: @THE-Spellchecker 154 total contributions
    THE-Spellchecker's avatar
    THE-Spellchecker's avatar
    @THE-Spellchecker

    154 total contributions

  3. Preview: @sharkipelago 4 total contributions
    sharkipelago's avatar
    sharkipelago's avatar
    @sharkipelago

    4 total contributions

  4. Preview: @christian.dinh 2492 total contributions
    christian.dinh's avatar
    christian.dinh's avatar
    @christian.dinh

    2492 total contributions

  5. Preview: Anonymous contributor 3126 total contributions
    Anonymous contributor's avatar
    Anonymous contributor

    3126 total contributions

  1. timm167's avatar
    timm167
  2. THE-Spellchecker's avatar
    THE-Spellchecker
  3. sharkipelago's avatar
    sharkipelago
  4. christian.dinh's avatar
    christian.dinh
  5. Anonymous contributor's avatar
    Anonymous contributor

Contribute to Docs

Learn Python on Codecademy

Morty Proxy This is a proxified and sanitized view of the page, visit original site.