获取异常详细信息

时间:2018-10-05 13:12:53

标签: python

我知道我可以在python中捕获异常,例如:

try:
    with open('file.log') as file:
        read_data = file.read()
except:
    print('Exception!')

但是如何获取异常类型或错误消息?

1 个答案:

答案 0 :(得分:1)

try:
    with open('file.log') as file:
        read_data = file.read()
except Exception as e:
    print(e)

您必须将Exception强制转换为变量。 Python documentation中有更多内容。