The only two possible types for an except
's expression are a class deriving from BaseException
, or a tuple composed of
such classes.
Trying to catch multiple exception in the same except
with a boolean expression of exceptions may not work as intended. The result of
a boolean expression of exceptions is a single exception class, thus using a boolean expression in an except
block will result in
catching only one kind of exception.
error = ValueError or TypeError
error is ValueError # True
error is TypeError # False
error = ValueError and TypeError
error is ValueError # False
error is TypeError # True
Note: In Python 2 it is possible to raise an exception from an old-style class that does not derive from
BaseException
.