Python User-defined Exception

By | May 23, 2020
Python User-defined Exception

Python User-defined Exception

In this article, we will discuss how can we create our own custom exception using some keywords in python.

In python, we can create our exception, and give them name whatever and raise them whenever we want. As we know, when the python interpreter finds any error and raises a statement in a program it throws an error and the program terminates itself.


It could be possible that when you create your own program you want some expectations which are not present in the standard python exception class, so to tackle this problem, you can create a custom exception and give them any name which fit your program.

To create a user-defined expectation we use class and inherit the Exception class itself.


class user_defined_error(Exception):

raise user_defined_error


raise user_defined_error


Behind the code:

In the above example, we have provided an idea of how can we form a user-defined exception by inheriting the Exception class.

Here we first defined a class by name user_defined_error and inherit the Exception class, so we can use the user_defined_error class with raise statement.

Why we use User-defined function

When we work on big projects where we want our own exceptions, for example, if you want to make a new library for python, so there you need to put some code which can catch the error and throw the appropriate message, to catch and raise the error we have exception handling, but what error message shown to the user, depends on the type of mistake done in the program.

When we import a module in a program, we notice that many new types of exceptions also introduced in our program, all those exceptions do not belong to the standard python exception so where they come from, all those exceptions created by the library developers.


Let’s see another example for the user-defined exception, so you could get a better idea of how to create and use it.

Here in this program, we will ask the user to enter a specific number if the user does not enter that number, we would throw a relevant error. We will also put some conditional statements in the program, which call different types of user-defined error we would create.


import random

class Less_then(Exception):
    ''' The number you have enterd is smaller than asked number '''

class Greater_then(Exception):
    ''' The number you have enterd is greater than asked number '''

i = random.randint(1,100)
    num =int(input("Enter {}:".format(i)))
    if num > i:
        raise Greater_then
    elif num < i:
        raise Less_then
        print("Well Done!")

except Greater_then:
    print("The value you have entered is greater than {}".format(i))

except Less_then:
    print("The value you have entered is less than {}".format(i))


Enter 91: 92

The value you have entered is greater than 91

Behind the code:

Here in the above example, we have created our own exceptions Greater_than and Less_than.  We have also used the exception handling statements (try and except) to catch and throw the error.

Leave a Reply

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