xxxxxxxxxx
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
xxxxxxxxxx
# Custom exception class
class CustomException(Exception):
pass
# Raise the custom exception
raise CustomException("This is a custom exception")
# Try-catch block to handle the custom exception
try:
# Code that may raise the custom exception
raise CustomException("This is a custom exception")
except CustomException as e:
# Handle the custom exception
print("Custom exception occurred:", e)