Python try…except comma vs 'as' in except

前端 未结 5 975
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 15:14

What is the difference between \',\' and \'as\' in except statements, eg:

try:
    pass
except Exception, exception:
    pass

and:

5条回答
  •  心在旅途
    2020-11-22 15:44

    If you want to support all python versions you can use the sys.exc_info() function like this:

    try:
        a = 1/'0'
    except (ZeroDivisionError, TypeError):
        e = sys.exc_info()[1]
        print(e.args[0])
    

    (source:http://python3porting.com/noconv.html)

提交回复
热议问题