【发布时间】:2022-01-07 01:36:48
【问题描述】:
我正在尝试为我正在编写的代码提取信息。比如我写这段代码的时候:
code = "hello"
code[10]
我从 python 得到的输出如下:
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_24520/526779498.py in <module>
----> 1 code[10]
IndexError: string index out of range
但是当我在代码上实现 try except 循环时:
code = "hello"
try:
code[10]
except Exception as e:
print(e)
我的输出只显示我:
string index out of range
我需要做什么才能提取文本“IndexError”。此外,如果有任何库可用于提取 python 错误以进行日志记录,请告诉我。谢谢。
【问题讨论】:
-
如果你只想打印异常类型,你可以使用
print(e.__class__.__name__)
标签: python python-3.x error-handling