相关疑难解决方法(0)

在一行中捕获多个异常(块除外)

我知道我能做到:

try:
    # do something that may fail
except:
    # do this if ANYTHING goes wrong
Run Code Online (Sandbox Code Playgroud)

我也可以这样做:

try:
    # do something that may fail
except IDontLikeYouException:
    # say please
except YouAreTooShortException:
    # stand on a ladder
Run Code Online (Sandbox Code Playgroud)

但如果我想在两个不同的例外中做同样的事情,我现在能想到的最好的就是这样做:

try:
    # do something that may fail
except IDontLikeYouException:
    # say please
except YouAreBeingMeanException:
    # say please
Run Code Online (Sandbox Code Playgroud)

有什么办法我可以做这样的事情(因为两个例外的行动都是say please):

try:
    # do something that may fail
except IDontLikeYouException, YouAreBeingMeanException:
    # say please
Run Code Online (Sandbox Code Playgroud)

现在这真的不起作用,因为它符合以下语法:

try:
    # do something that may fail
except …
Run Code Online (Sandbox Code Playgroud)

python exception-handling exception

2521
推荐指数
6
解决办法
72万
查看次数

标签 统计

exception ×1

exception-handling ×1

python ×1