Converting Exception to a string in Python 3

后端 未结 5 1904
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 23:08

does anyone have an idea, why this Python 3.2 code

try:    
    raise Exception(\'X\')
except Exception as e:
    print(\"Error {0}\".format(str(e)))

5条回答
  •  庸人自扰
    2021-02-04 23:54

    Try this, it should work.

    try:    
        raise Exception('X')
    except Exception as e:
        print("Error {0}".format(str(e.args[0])).encode("utf-8"))
    

    Considering you have only a message in your internal tuple.

提交回复
热议问题