Raising Exceptions
Use raise to trigger an exception intentionally, signalling that something is wrong in your logic.
Use raise to trigger an exception intentionally, signalling that something is wrong in your logic.
def set_age(age):
if age < 0:
raise ValueError("Age cannot be negative")
return age
try:
set_age(-5)
except ValueError as e:
print(e)
Raise exceptions to enforce business rules in your functions.