Catch KeyError in Python

前端 未结 7 1948
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 14:33

If I run the code:

connection = manager.connect(\"I2Cx\")

The program crashes and reports a KeyError because I2Cx doesn\'t exist (it should be

7条回答
  •  旧时难觅i
    2021-02-01 14:49

    Try print(e.message) this should be able to print your exception.

    try:
        connection = manager.connect("I2Cx")
    except Exception, e:
        print(e.message)
    

提交回复
热议问题