121. Custom Exception Handling
class CustomError(Exception):
"""Base class for other exceptions"""
pass
def check_value(value):
if value < 0:
raise CustomError("Value cannot be negative")
try:
check_value(-1)
except CustomError as e:
print(f"Error: {e}")class ValueTooLowError(Exception):
def __init__(self, message="Value is too low"):
self.message = message
super().__init__(self.message)
def process_value(value):
if value < 10:
raise ValueTooLowError("Value must be greater than or equal to 10")
return value * 2
try:
result = process_value(5)
except ValueTooLowError as e:
print(f"Error: {e}")Last updated