I am learning from the book Python for Absolute Beginners and am up to the chapter on exceptions. The authors explanations have been increasingly short, and with this piece of code, i am completely confused and there is no explanation! Can someone explain please, line by line?
#!/usr/bin/env python
store = []
try: {}["foo"]
except KeyError as e: store.append(e)
try: 1/0
except ZeroDivisionError as e: store.append(e)
try: "".bar()
except AttributeError as e: store.append(e)
for exceptionobject in store:
ec = exceptionobject.__class__
print(ec.__name__)
indent = " +-"
while ec.__bases__:
ec = ec.__bases__[0]
print(indent + ec.__name__)
indent = " " + indent
At this point the list has three elements, which are exception objects.
The result should look something like this:
showing a part of the exception hierarchy.