Answers for "raise a custom exception python"

7

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)
Posted by: Guest on August-31-2020
11

raise a custom exception python

raise Exception "FileError: \nCould not read file."
Posted by: Guest on May-24-2021
2

python custom exception

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
Posted by: Guest on April-19-2021
6

create custom exception python

class CustomError(Exception):
  	pass
Posted by: Guest on March-05-2020

Code answers related to "raise a custom exception python"

Python Answers by Framework

Browse Popular Code Answers by Language