我知道我可以在python中捕获异常,例如:
try:
with open('file.log') as file:
read_data = file.read()
except:
print('Exception!')
但是如何获取异常类型或错误消息?
答案 0 :(得分:1)
try:
with open('file.log') as file:
read_data = file.read()
except Exception as e:
print(e)
您必须将Exception强制转换为变量。 Python documentation中有更多内容。