raise
statement. Most commonly raised are ValueError
and AttributeError
.## Example 1: Manually raising a exception
def my_fun(a):
try:
if a == 20:
raise ValueError("I don't want number 20")
# or not specifying a specific exception like,
# "raise Exception('my message')"
print(f"{a} should be fine")
except ValueError as v:
print(v)
my_fun(20) # I don't want number 20
my_fun(10) # 10 should be fine