代码之家  ›  专栏  ›  技术社区  ›  vico

获取异常详细信息[重复]

  •  0
  • vico  · 技术社区  · 6 年前

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

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

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Josh Laird    6 年前
    try:
        with open('file.log') as file:
            read_data = file.read()
    except Exception as e:
        print(e)
    

    必须将异常强制转换为变量。这个和更多是在 Python documentation