X Tutup
Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 1.29 KB

File metadata and controls

45 lines (34 loc) · 1.29 KB

Python Cheat Sheet

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.

Read It

Exception Handling

Basic exception handling

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))

Final code in exception handling

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))
X Tutup