We have learned about Exception in the article Errors and Exceptions in Python. Then, handling Exceptions was introduced in the article Handling exceptions in Python. Python also provides many built-in Exceptions to help us catch Exceptions. However, sometimes we also need to create Exceptions to serve our purposes. These Exceptions are called User-Defined Exception in Python.
1. Creating a User-Defined Exception in Python
We can create a new User-Defined Exception in the Python class by inheriting from the Exception class.
# class MyError inherits from class Exception
class MyError(Exception):
# constructor method
def __init__(self, message):
self.message = message
# __str__ display function
def __str__(self):
return self.message
try:
# throw MyError Exception
raise MyError("User-Defined Exception.")
except MyError as error:
print('A New Exception occured:', error.message)
Result
A New Exception occured: User-Defined Exception.
When a User-Defined Exception inherits from the Exception class, it can redefine some of the methods of Exception that it inherits.
class MonthWrongValueError(Exception):
"""Exception raised for errors in the input month.
Attributes:
month -- input month which caused the error
message -- explanation of the error
"""
def __init__(self, month, message="Month is not in (1, 12) range"):
self.month = month
self.message = message
super().__init__(self.message)
def __str__(self):
return f'{self.month} -> {self.message}'
month_input = int(input("Enter month: "))
if not 1 <= month_input <= 12:
raise MonthWrongValueError(month_input)
else:
print("month: ", month_input)
Result 1
month: 10
Result 2
Traceback (most recent call last):
File "c:\python-examples\main.py", line 18, in <module>
raise MonthWrongValueError(month_input)
__main__.MonthWrongValueError: 25 -> Month is not in (1, 12) range
In the above example, the MonthWrongValueError class inherits from the Exception class. The MonthWrongValueError class has redefined the __init__() and __str__() functions of the Exception class to meet its error message output requirements.
2. Inheriting a User-Defined Exception
Other User-Defined Exception classes can inherit a User-Defined Exception class we define ourselves.
# class MyError inherits from class Exception
class MyError(Exception):
# constructor method
def __init__(self, message):
self.message = message
# __str__ display function
def __str__(self):
return self.message
# class ValueTooSmallError inherits from class MyError
class ValueTooSmallError(MyError):
"""Raised when the input value is too small"""
pass
# class ValueTooLargeError inherits from class MyError
class ValueTooLargeError(MyError):
"""Raised when the input value is too large"""
pass
# you need to guess this number
number = 5
# user guesses a number until user gets it right
while True:
try:
i_num = int(input("Enter a number: "))
if i_num < number:
raise ValueTooSmallError("This value is too small, try again!")
elif i_num > number:
raise ValueTooLargeError("This value is too large, try again!")
break
except ValueTooSmallError as error:
print(error.message)
except ValueTooLargeError as error:
print(error.message)
print("Congratulations! You guessed it correctly.")
Result
This value is too large, try again!
Enter a number: 7
This value is too large, try again!
Enter a number: 3
This value is too small, try again!
Enter a number: 5
Congratulations! You guessed it correctly.
In the above example, we have a User-Defined Exception class called MyError. MyError is also inherited by two other User-Defined Exception classes, ValueTooSmallError and ValueTooLargeError.
In conclusion, user-defined exceptions can make our code readable and error messages more informative. By creating exceptions specific to our program, we can provide more meaningful feedback to our users.