tryexception的问题

class Ex(Exception):
    def __init__(self,msg):
        Exception.__init__(self,msg+msg)
        self.args=(msg,)
try:
    raise Ex('ex')
except Ex as e:
    print(e)
except Exception as e:
    print(e)

ouput:ex
为什么不是exex
try:
    raise Ex('ex')
except Ex as e:
    print(e)
except Exception as e:
    print(e)

raise Ex('ex')抛出Ex异常,异常信息为ex
回到调用的地方,except Ex能够捕获Ex异常,所以执行了第一个except异常,输出异常信息为ex
如果执行下面父类异常,也是输出ex

except Exception as e:

因为你 raise Ex('ex') 传递给e就是'ex'
except Ex as e:中
print(e)输出e就是ex
try: 之后多个except 只要有一个满足条件:其它的except 就不判断了:
except Ex as e:已经满足条件
except Exception as e:就不判断了

这与if elif 是一个道理

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632