Question: 1 . Create a Python class named 'InvalidAgeError' that inherits from the built - in 'Exception' class. a . Define an ' _ _ init

1. Create a Python class named 'InvalidAgeError' that inherits from the built-in 'Exception' class.
a. Define an '__init__' method within 'InvalidAgeError' to customize the error message.
2. Implement a function named 'validate_age'.
a. Define the function to take an 'age' parameter.
b. Use a 'try' block to check if the 'age' is less than 18.
i. If 'age' is less than 18, raise an instance of 'InvalidAgeError'.
ii. If 'age' is not less than 18, print 'Age is valid.'.
c. Use an 'except' block to catch 'InvalidAgeError' and print the custom error message.
3. Outside the function:
a. Call 'validate_age' with the following age values.
i. validate_age(22)
ii. validate_age(16)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!