Standard Python Exception List for Programmers to Raise



In Python, an exception is an error that occurs at the time of execution. These will terminate the program abruptly. If you are a programmer looking to raise meaningful exceptions, it is important to know the list of standard exceptions Python provides.

What Are Python Exceptions?

Exceptions in Python are built-in classes that inherit from the base class BaseException. They help you to manage errors like missing files, wrong data types, division by zero, or custom business logic violations.

Where Can You Find the Full List?

The complete list of built-in exceptions is available in the official Python documentation. You can find it here -

https://docs.python.org/3/library/exceptions.html

This page includes descriptions for every standard exception class that Python has, including their inheritance hierarchy and usage notes.

Commonly Raised Built-in Exceptions

Here are some of the most commonly raised exceptions in Python that you can use directly in your code -

  • ValueError: Raised when a function gets an argument of the correct type but an inappropriate value.
  • TypeError: Raised when an operation or function is applied to an object of an inappropriate type.
  • KeyError: Raised when a dictionary key is not found.
  • IndexError: Raised when a list index is out of range.
  • ZeroDivisionError: Raised when trying to divide by zero.
  • FileNotFoundError: Raised when trying to access a file that doesn't exist.
  • PermissionError: Raised when trying to do something without proper permissions.

Example: Raising a Standard Exception

Following is an example of raising a standard exception in Python -

def set_age(age):
   if age < 0:
      raise ValueError("Age cannot be negative")
   print("Age is", age)

set_age(-5)

We get the output as shown below -

Traceback (most recent call last):
  File "/home/cg/root/681af9f2c7256/main.py", line 6, in <module>
set_age(-5)
  File "/home/cg/root/681af9f2c7256/main.py", line 3, in set_age
    raise ValueError("Age cannot be negative")
ValueError: Age cannot be negative

How to Choose the Right Exception?

When raising an exception in your program, choose the one that best describes the kind of error -

  • If input is missing or invalid, consider ValueError.
  • If an operation isn't allowed on a type, use TypeError.
  • If it's a logical error that doesn't match any existing exception, consider creating a custom exception.

Creating Custom Exceptions

You can also define your own exceptions by extending the built-in Exception class -

class CustomError(Exception):
   pass

raise CustomError("Something custom went wrong!")

The error obtained is as follows -

Traceback (most recent call last):
  File "/home/cg/root/681af9f2c7256/main.py", line 4, in <module>
    raise CustomError("Something custom went wrong!")
CustomError: Something custom went wrong!
Updated on: 2025-05-14T15:21:35+05:30

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements