2024-05-23 21:26:35 发布
网友
如何在Python的try..except块中获取异常的名称
try..except
try: #code that throws errors except Exception as e: #A broad except block to catch all errors #Handling the error
有时,当有很多异常需要处理时,捕获Exception似乎很容易(尽管不鼓励)。但是我找不到异常的名称,有什么办法吗
Exception
使用以下命令:
type(e).__name__
或
type(e).__class__.name
type(e).__class__.qualname
假设“name”表示“type”,请在except块中尝试type(e)
except
type(e)
使用以下命令:
或
或
假设“name”表示“type”,请在
except
块中尝试type(e)
相关问题 更多 >
编程相关推荐