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

Question:

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?

Asked By: Nathan

||

Answers:

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

Answered By: Fábio Diniz

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

Answered By: nmichaels
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.