我知道我可以做到:
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
我有没有办法做到这一点(因为在两种例外情况下都要采取的行动是说请):
try:
# do something that may fail
except IDontLikeYouException, YouAreBeingMeanException:
# say please
现在这真的行不通了,因为它符合以下语法:
try:
# do something that may fail
except Exception, e:
# say please
所以,我试图抓住这两个截然不同的例外并没有成功。
有办法做到这一点吗?