正在获取异常的名称以及错误

2024-05-23 21:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Python的try..except块中获取异常的名称

try:
    #code that throws errors
except Exception as e:
    #A broad except block to catch all errors
    #Handling the error

有时,当有很多异常需要处理时,捕获Exception似乎很容易(尽管不鼓励)。但是我找不到异常的名称,有什么办法吗


Tags: to名称thatasexceptioncodeallblock
2条回答

使用以下命令:

type(e).__name__

type(e).__class__.name

type(e).__class__.qualname

假设“name”表示“type”,请在except块中尝试type(e)

相关问题 更多 >