Proper error handling makes your code robust.
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
else:
print("No errors occurred")
finally:
print("Cleanup code")
# Custom exceptions
class ValidationError(Exception):
pass
def validate_age(age):
if age < 0:
raise ValidationError("Age cannot be negative")