在一行中捕获多个异常(块除外)
我知道我可以做到:
try:
# do something that may fail
except:
# do this if ANYTHING goes wrong
我也可以这样做:
try:
# do something that may fail
except IDontLikeYouException:
# say please
except YouAreTooShortException:
# stand on a ladder
但是,如果我想在两个不同的例外中做同样的事情,我现在能想到的最好的事情就是这样做:
try:
# do something that may fail
except IDontLikeYouException:
# say please
except YouAreBeingMeanException:
# say please
有没有办法让我做这样的事情(因为在两个例外中要执行的操作是):say please
try:
# do something that may fail
except IDontLikeYouException, YouAreBeingMeanException:
# say please
现在这真的不起作用,因为它与以下语法相匹配:
try:
# do something that may fail
except Exception, e:
# say please
因此,我捕捉两个截然不同的异常的努力并没有完全实现。
有没有办法做到这一点?