datetime

Introduction

try,except,else,finally

try:
    print(x)
except:
    print("exception occured")

can do different types of exception

try:
    c = a // b
except ZeroDivisionError:
    print("Dividing by zero")
except:
    print("Some other problem happened")

Can add else for if no exceptions

try:
    print(x)
except:
    print("Exception happend")
else:
    print("No exception happened")

Can add "finally" black to always execute regardly of type of exception or if it just did else

try:
    print(x)
except:
    print("Exception happened")
else:
    print("No exception happened")
finally:
    print("This prints if an exception happens or not")

Manually raising exceptions with "raise"

We can manually raise exceptions

raise Exception
raise ZeroDivisionError

assert

assert here too: assert(False) raises AssertionError

with

"with" function easier to use than try except released objects afterwards, so need to worry less about clean up if eg opening files