Others answered your second question, so I will be the first to do:
except NameError and TypeError as e:
exactly the same as:
except TypeError as e:
It will not catch NameError . What for? Since NameError is true, therefore and evaluates and returns its second argument, TypeError . Try:
print NameError and TypeError
Yes, exceptions in the except clause are evaluated at runtime, like any other expression. In fact, Python does not even require them to be exceptions (however, if they are not, they wonβt understand anything anymore because raise requires exceptions ... well, you can use strings, but they are deprecated).
For this reason, you must provide a tuple of exceptions to catch:
except (TypeError, ValueError) as e:
Other forms may be accepted by Python as a valid syntax, but probably will not work as you might expect.
source share