What's wrong with my except? [duplicate]

蹲街弑〆低调 提交于 2019-12-18 10:34:24

问题


I've got a SyntaxError on my except:

try:
    opts, args = getopt.getopt(sys.argv[1:], 'P:D:H:d:u:p:nvhmJi:c:Ml:TB:',
            ['host=', 'port=', 'directory=', 'user=', 'password=',
             'daemon=', 'noauth', 'help', 'verbose', 'mysql', 
             'icounter=', 'config=', 'nolock', 'nomime', 'loglevel', 'noiter',
             'baseurl='])
except getopt.GetoptError, e:
    print usage
    print '>>>> ERROR: %s' % str(e)
    sys.exit(2)

I get the error:

File "main.py", line 199

except getopt.GetoptError, e:

SyntaxError: invalid syntax

Anyone have any idea?


回答1:


You use python3 and in python3 the raise syntax no longer accepts comma-separated arguments.

Use as instead:

except getopt.GetoptError as e:

This form is also backwards-compatible with 2.6 and 2.7.




回答2:


Your syntax is invalid for catching the exception

You should have written except getopt.GetoptError as e: instead of except getopt.GetoptError, e:



来源:https://stackoverflow.com/questions/14908789/whats-wrong-with-my-except

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!