Python Custom Exception | Used Defined Exceptions

Posted in /  

Python Custom Exception | Used Defined Exceptions
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    In this tutorial, we will discuss Python custom exceptions and when to create one of them. There are many kinds of exceptions in Python. Most of them are built-in, and some come with third-party libraries.

    Here, the third-party exceptions mean when we install any module or package from the third party using pip install , and if that exception is related to the specific libraries, that exception is known as a third-party library exception.

    However, Python provides a feature by which we can make our own exceptions and give them any name. These exceptions are also known as user-defined exceptions.

    Python Custom Exception | How to Define?

    With the help of a class, we can create our own exceptions. The one thing to note here is that the class we create to make our own exception should be a child or subclass of Exception. At last, to create a custom exception, we must inherit the in-built Exception class.

    Syntax

    class custom_exception_name:
        pass
    raise  custom_exception_name (“Exception Message”)

    Example

    class OurException(Exception):                  # inheriting Exception
        pass
    inp = int(input("Enter 2: "))
    if inp !=2:
        raise OurException("We ask you to enter 2")

    Output

    Enter 2: 3
    Traceback (most recent call last):
    raise OurException("We ask you to enter 2")
    __main__.OurException: We ask you to enter 2

    Behind the code

    In the above code, first, we made a class OurException , which has an Exception as an argument. By writing the Exception as an argument, the class OurException inherits the property of the Exception class.

    When we work on a big project, we create our own exceptions; this technique is very handy. Even when developers create libraries for Python, in their package, they create a separate Python file that contains all custom errors.

    Custom or User-Defined Exception

    Let’s upgrade the code we have written above, now, we will ask the user to enter a number, and the program will execute again and again until the user enters 2 in the input box.

    Example:

    class Not_2_Error(Exception):
        pass
    while True:
        try:
            inp = int(input("Enter 2: "))
            if inp != 2:
                raise Not_2_Error("Please Enter 2")
            else:
                break     #It will break the while loop
        except Not_2_Error:
            print("You entered a wrong Number we asked for 2, you have entered:", inp)
    print("You have entered 2 now program terminate here")

    Output

    Enter 2: 3
    You entered a wrong Number we asked for 2, you have entered: 3
    
    Enter 2: 4
    You entered a wrong Number we asked for 2, you have entered: 4
    
    Enter 2: 5
    You entered a wrong Number we asked for 2, you have entered: 5
    
    Enter 2: 2
    You have entered 2 now program terminate here

    Conclusion

    In Python, you can create your own exceptions by creating a new exception class. These custom exceptions are also referred to as user-defined exceptions. After reading this article, you will come to know how to create custom exceptions and when to create them. The examples above will give you a clear idea of Python custom exceptions.

    People are also reading:

    Leave a Comment on this Post

    0 Comments