Catch KeyError in Python

前端 未结 7 1835
没有蜡笔的小新
没有蜡笔的小新 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条回答
  • 2021-02-01 15:00

    I am using Python 3.6 and using a comma between Exception and e does not work. I need to use the following syntax (just for anyone wondering)

    try:
        connection = manager.connect("I2Cx")
    except KeyError as e:
        print(e.message)
    
    0 讨论(0)
提交回复
热议问题