How to Define Custom Exceptions in Python? (With Examples)
Wed Dec 22 2021 14:39:14 GMT+0000 (Coordinated Universal Time)
Saved by
[deleted user]
class SalaryNotInRangeError(Exception):
"""Exception raised for errors in the input salary.
Attributes:
salary -- input salary which caused the error
message -- explanation of the error
"""
def __init__(self, salary, message="Salary is not in (5000, 15000) range"):
self.salary = salary
self.message = message
super().__init__(self.message)
def __str__(self):
return f'{self.salary} -> {self.message}'
salary = int(input("Enter salary amount: "))
if not 5000 < salary < 15000:
raise SalaryNotInRangeError(salary)
content_copyCOPY
https://www.programiz.com/python-programming/user-defined-exception
Comments