-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprac16.py
More file actions
24 lines (21 loc) · 727 Bytes
/
Copy pathprac16.py
File metadata and controls
24 lines (21 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Practical 16 - User Defined Exception Handling
# Step 1: Define custom exception class
print("****** Madanraj Sagar ******")
class UnderAgeError(Exception):
def __init__(self, age):
self.age = age
super().__init__(f"Access Denied: Age {age} is below 18.")
# Step 2: Function to check age
def check_age(age):
if age < 18:
raise UnderAgeError(age) # Raise custom exception
else:
print("Access Granted: You are eligible.")
# Step 3: Main code with exception handling
try:
user_age = int(input("Enter your age: "))
check_age(user_age)
except UnderAgeError as e:
print("Custom Exception Caught:", e)
except ValueError:
print("Invalid input! Please enter a number.")