User-Defined Exceptions in Python

Introduction

In Python, exceptions are events that disrupt the normal flow of a program. When such an event occurs, the program crashes unless the exception is handled. Python has many built-in exceptions, such as ValueError, TypeError, and IndexError. However, there are situations where these built-in exceptions may not adequately represent the specific error conditions of your program. In such cases, you can create your own user-defined exceptions.

Creating User-Defined Exceptions

To create a user-defined exception in Python, you need to define a new class that inherits from the built-in Exception class or one of its subclasses. By inheriting from Exception, your custom exception class will be compatible with Python’s exception handling framework.

Here is the basic syntax for creating a user-defined exception:

class MyCustomError(Exception):
    """Base class for other exceptions"""
    pass

Example 1: Basic User-Defined Exception

Let’s create a simple custom exception to handle errors when a specific condition is not met.

class NegativeNumberError(Exception):
    """Exception raised for errors in the input if the number is negative."""

    def __init__(self, number, message="Number cannot be negative"):
        self.number = number
        self.message = message
        super().__init__(self.message)

# Example of usage
def check_positive(number):
    if number < 0:
        raise NegativeNumberError(number)

try:
    check_positive(-5)
except NegativeNumberError as e:
    print(f"Error: {e}")

Example 2: Hierarchy of User-Defined Exceptions

Sometimes, it is beneficial to create a hierarchy of exceptions, especially when you want to handle different types of errors in different ways.

class CustomError(Exception):
    """Base class for other exceptions"""
    pass

class InputError(CustomError):
    """Exception raised for errors in the input."""

    def __init__(self, expression, message="Invalid input"):
        self.expression = expression
        self.message = message
        super().__init__(self.message)

class CalculationError(CustomError):
    """Exception raised for errors in the calculation."""

    def __init__(self, calculation, message="Error in calculation"):
        self.calculation = calculation
        self.message = message
        super().__init__(self.message)

# Example of usage
def process_input(input_value):
    if not isinstance(input_value, int):
        raise InputError(input_value, "Input must be an integer")

def calculate_division(a, b):
    if b == 0:
        raise CalculationError(f"{a} / {b}", "Division by zero")
    return a / b

try:
    process_input("ten")
except InputError as e:
    print(f"Input Error: {e}")

try:
    calculate_division(10, 0)
except CalculationError as e:
    print(f"Calculation Error: {e}")

Handling User-Defined Exceptions

User-defined exceptions are handled in the same way as built-in exceptions. You can use a try-except block to catch and handle these exceptions.

try:
    # Code that might raise an exception
    process_input("ten")
except InputError as e:
    print(f"Caught an InputError: {e}")
except CalculationError as e:
    print(f"Caught a CalculationError: {e}")
except CustomError as e:
    print(f"Caught a CustomError: {e}")

Adding Custom Methods to Exceptions

You can add custom methods to your exception classes to provide additional functionality.

class InvalidAgeError(Exception):
    def __init__(self, age, message="Age must be between 0 and 120"):
        self.age = age
        self.message = message
        super().__init__(self.message)

    def log_error(self):
        print(f"Logging error: Age {self.age} is not valid.")

# Example of usage
def set_age(age):
    if age < 0 or age > 120:
        error = InvalidAgeError(age)
        error.log_error()
        raise error

try:
    set_age(150)
except InvalidAgeError as e:
    print(f"Error: {e}")

Conclusion

User-defined exceptions provide a way to create more specific and meaningful error messages in your programs. By defining your own exceptions, you can handle error conditions in a more precise and readable manner. Remember to always document your custom exceptions so that their purpose is clear to anyone reading your code.

Hello, I’m Anuj. I make and teach software.

My website is free of advertisements, affiliate links, tracking or analytics, sponsored posts, and paywalls.
Follow me on LinkedIn, X (twitter) to get timely updates when I post new articles.
My students are the reason this website exists. ❤️

Feedback Display