Basic cheatsheet for Python mostly based on the book written by Al Sweigart, Automate the Boring Stuff with Python under the Creative Commons license and many other sources.
def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError as e:
print('Error: Invalid argument: {}'.format(e))
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
Code inside the finally
section is always executed, no matter if an exception has been raised or
not, and even if an exception is not caught.
def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError as e:
print('Error: Invalid argument: {}'.format(e))
finally:
print("-- division finished --")
print(spam(12))
print(spam(0))