Python User-defined Exception

By | September 14, 2021
Python User-defined Exception

In this article, we will discuss how can we create our own custom exception using some keywords in python i.e. Python user-defined exception. Having the ability to develop custom exceptions is an important skill for any professional developer.

Python User-defined Exception

Python has a galore of built-in exceptions. In Python, we can create exceptions and give them names of our choice. Also, we can 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.

Vamware

It could be possible that when you create your own program, you want some expectations that are not present in the standard python exception class. To tackle this problem, you can create a Python user-defined exception and give it a name you like.

To create a user-defined exception in Python we use class and inherit the Exception class itself. Observe the following program to understand it better.

Example:

class user_defined_error(Exception):
    pass

raise user_defined_error

Output:

raise user_defined_error

__main__.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 with the name user_defined_error and inherit the Exception class, so we can use the user_defined_error class with a raise statement.

Why We Use a User-defined Exception?

When we work on big projects where we want our own exceptions we need to use a Python user-defined function. For example, when you want to make a new library for python, there you need to put code that can catch the error and throw the appropriate message.

To catch and raise the error, we have exception handling. Also, the error message that is shown to the user depends on the type of mistake done in the program.

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

Example:

Let’s see another example for the Python 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 an error. We will also put some conditional statements in the program, which call different types of user-defined errors that we will create. Here is the code for the program:

import random

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

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

i = random.randint(1,100)
try:
    num =int(input("Enter {}:".format(i)))
    if num > i:
        raise Greater_then
    elif num < i:
        raise Less_then
    else:
        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))

Output:

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 by the name Greater_than and Less_than. We have also used the exception handling statements (try and except) to catch and throw the error.

Conclusion

Exception handling is an important aspect while dealing with big programs. Thus, as a professional Python developer, you must be capable of creating your own Python user-defined exceptions.

Therefore, Python lets you create your own custom exceptions to deal with situations for which Python usually doesn’t have any provision for exception handling.

Leave a Reply

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