As the IndexError
occurs, it goes to the except
, so the 4/0
is not executed, the ZeroDivisionError
doesn't occur, to get both executed, use 2 different try-except
try:
a = [1, 2]
print(a[3])
except IndexError as e:
print(e)
try:
4 / 0
except ZeroDivisionError as e:
print(e)
Giving
list index out of range
division by zero
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…