I was going through the Documentation of exceptions in python :-(https://docs.python.org/2/tutorial/classes.html#exceptions-are-classes-too)
I can't seem to find how this code works
class B:
pass
class C(B):
pass
class D(C):
pass
for c in [B, C, D]:
try:
raise c()
except D:
print "D"
except C:
print "C"
except B:
print "B"
Output:-
B
C
D
What's "pass" doing in the classes? Just by going over the documentation all i can get is objects of all the classes are made( Object creation order :- B,C and D) and exceptions are raised in their names, if that's the order then it explains the output B,C,D.
But if we replace excpet B with except D the whole output is changed.
class B:
pass
class C(B):
pass
class D(C):
pass
for c in [B, C, D]:
try:
raise c()
except B:
print "B"
except C:
print "C"
except D:
print "D"
Output:-
B
B
B
Now this makes my head spinning:/
How's the order of "except" changing the output?
I know i am missing something from the documentation, maybe because it's not very clear :(