Skip to content

Python exception types

  • by

Python exception types refer to the predefined categories or classes of exceptions that can occur during the execution of a Python program. These exception types are used to identify and handle different types of errors and exceptional situations that may arise in your code. Some commonly used Python exception types include:

Here’s a tabular format of some commonly used Python exception types:

Exception TypeDescription
ExceptionBase class for all built-in exceptions.
SyntaxErrorRaised when there is a syntax error in the code.
TypeErrorRaised when an operation or function is applied to an inappropriate type.
NameErrorRaised when a local or global name is not found.
FileNotFoundErrorRaised when a file or directory is requested, but it cannot be found.
ValueErrorRaised when a function receives an argument of the correct type but an invalid value.
IndexErrorRaised when a sequence subscript is out of range.
KeyErrorRaised when a dictionary key is not found.
ZeroDivisionErrorRaised when the second operand of a division or modulo operation is zero.
AssertionErrorRaised when an assert statement fails.

These are just a few examples of the many exception types available in Python. You can also define your custom exception types by creating a new class that inherits from the Exception base class or any other built-in exception class.

Python exception types examples

Here’s an example that demonstrates different Python exception types and how they can be handled:

try:
    # Code that may raise exceptions
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))

    result = num1 / num2
    print("Result:", result)

except ValueError:
    print("Error: Invalid input. Please enter a valid number.")

except ZeroDivisionError:
    print("Error: Division by zero is not allowed.")

except NameError:
    print("Error: Variable is not defined.")

except Exception as e:
    print("Error:", str(e))

else:
    print("No exceptions occurred.")

finally:
    print("Exception handling complete.")

Output:

Python exception types

In this example, we have multiple exception types being handled:

  1. ValueError: If the user enters a non-numeric value, a ValueError will be raised when converting the input to an integer.
  2. ZeroDivisionError: If the user enters 0 as the second number, a ZeroDivisionError will be raised when dividing num1 by num2.
  3. NameError: The line print(undefined_variable) will raise a NameError because undefined_variable is not defined.
  4. Exception (catch-all): If any other type of exception occurs, it will be caught by the generic except Exception block, which will print the error message.

The else block is executed if no exceptions occur, and the finally block is always executed, regardless of whether an exception occurred or not.

Note: Remember to handle exceptions appropriately based on your specific requirements and provide meaningful error messages to users.

Comment if you have any doubts or suggestions on this Python exception-handling topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *