我知道我可以做到:
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
因此,我捕捉两个截然不同的异常的努力并未完全实现。
有没有办法做到这一点?