create custom exception python
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)
4.33
3
class UnderAge(Exception):
pass
def verify_age(age):
if int(age) < 18:
raise UnderAge
else:
print('Age: '+str(age))
# main program
verify_age(23) # won't raise exception
verify_age(17) # will raise exception
Thank you!
3
0
3.8
10
class CustomError(Exception):
pass
Thank you!
10
0
3.57
7
raise Exception "FileError: \nCould not read file."
Thank you!
7
0
Are there any code examples left?
New code examples in category Python