except
General format:
try:
a = 10 / 0
except ZeroDivisionError as err:
print("Error " + str(err))
else:
print("It's ok...") # Else is executed if try statement passes without exception
finally: # Finally is executed either way
print("Finalizing")
raise
Assert is mostly intended for trapping user-defined constraints, because Python traps programming errors itself,
General format:
try:
a = 10 / 0
except ZeroDivisionError as err:
print("Error " + str(err))
else:
print("It's ok...") # Else is executed if try statement passes without exception
finally: # Finally is executed either way
print("Finalizing")
Python 3.X catching an exception named Exception ignores exceptions related to system exits.
raise
Assert is mostly intended for trapping user-defined constraints, because Python traps programming errors itself,
Assertion example:
def f(x):
assert x < 0, 'x must be negative'
return x ** 2
print(f(10))
Assertions can be skipped by running:
$ python -O raise.py
$ python -O raise.py