开发者

In Python, what's the difference between 'except Exception as e' and 'except Exception, e' [duplicate]

开发者 https://www.devze.com 2023-02-13 23:58 出处:网络
This question already has answers here: Python try...except comma vs 'as' in except 开发者_运维百科
This question already has answers here: Python try...except comma vs 'as' in except 开发者_运维百科 (5 answers) Closed 7 years ago.

In python, there are two ways to catch an exception

except Exception, e:

except Exception as e:

It seems like 'as e' is the one to use going forward. In what version of python did this change? Any idea why?


This PEP introduces changes intended to help eliminate ambiguities in Python's grammar, simplify exception classes, simplify garbage collection for exceptions and reduce the size of the language in Python 3.0.

PEP 3110: "Catching Exceptions in Python 3000"


Short answer for the why: Exception, e and Exception, TypeError are hard to tell apart. Long answer: what Ignacio said.


The first proposal for using the "as" is here: http://mail.python.org/pipermail/python-dev/2006-March/062449.html. They thought it would be more intuitive to read the code

0

精彩评论

暂无评论...
验证码 换一张
取 消