在 Python 中,“  例外”和“例外”之間有什麼不同

在 python 中,有两种捕获异常的方法

except Exception, e:


except Exception as e:

看起来“ as e”是用来向前看的。在 Python 的哪个版本中,这种情况发生了变化?知道为什么吗?

94786 次浏览

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"

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

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