Python 2.x gotchas and landmines

后端 未结 23 2270
北恋
北恋 2020-11-28 17:47

The purpose of my question is to strengthen my knowledge base with Python and get a better picture of it, which includes knowing its faults and surprises. To keep things sp

23条回答
  •  [愿得一人]
    2020-11-28 18:18

    try:
        int("z")
    except IndexError, ValueError:
        pass
    

    reason this doesn't work is because IndexError is the type of exception you're catching, and ValueError is the name of the variable you're assigning the exception to.

    Correct code to catch multiple exceptions is:

    try:
        int("z")
    except (IndexError, ValueError):
        pass
    

提交回复
热议问题