263

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

try:
    pass
except Exception, exception:
    pass

and:

try:
    pass
except Exception as exception:
    pass

Is the second syntax legal in 2.6? It works in CPython 2.6 on Windows but the 2.5 interpreter in cygwin complains that it is invalid.

If they are both valid in 2.6 which should I use?

Peter Graham
  • 11,323
  • 7
  • 40
  • 42

5 Answers5

305

The definitive document is PEP-3110: Catching Exceptions

Summary:

  • In Python 3.x, using as is required to assign an exception to a variable.
  • In Python 2.6+, use the as syntax, since it is far less ambiguous and forward compatible with Python 3.x.
  • In Python 2.5 and earlier, use the comma version, since as isn't supported.
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 17
    Using `as` is the only way to assign the exception to a local in Python 3.x. But `as` is not _required_, since you don't have to specify it if you don't need it at all. – mercator Apr 13 '15 at 13:20
41

Yes it's legal. I'm running Python 2.6

try:
    [] + 3
except Exception as x:
    print "woo hoo"

>>> 
woo hoo

Update: There is another reason to use the as syntax. Using , makes things a lot more ambiguous, as others have pointed out; and here's what makes the difference. As of Python 2.6, there is multicatch which allows you to catch multiple exceptions in one except block. In such a situation, it's more expressive and pythonic to say

except (exception1, exception2) as e

rather than to say

except (exception1, exception2), e

which would still work

Community
  • 1
  • 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 14
    Python versions *before* 2.6 allowed you to use a tuple to catch multiple exception types too. But the syntax was confusing; you'd use `except NameError, ValueError:` then wondered why the `ValueError` exception was never being caught. Or used `except (NameError, e):` and were left with a `NameError` for `'e'`! – Martijn Pieters Dec 14 '13 at 11:41
17

the "as" syntax is the preferred one going forward, however if your code needs to work with older Python versions (2.6 is the first to support the new one) then you'll need to use the comma syntax.

endolith
  • 25,479
  • 34
  • 128
  • 192
Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113
10

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)

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
pthomaid
  • 146
  • 2
  • 7
  • 2
    Note: There are some corner cases here to be aware of. In Py3, the `as` target (along with the rest of the exception info) is implicitly `del`ed as soon as you exit the `except` block (this prevents some cyclic references involved in the stored traceback from delaying the release of memory until the cyclic GC gets around to running). So this equivalent code is slightly less equivalent, unless you use a `try/finally` within the `except` block to ensure `del e` is performed before exiting the `except` block. – ShadowRanger Apr 25 '17 at 21:42
0

As of Python 3.7 (not sure about other versions) the 'comma' syntax is not supported any more:

Source file exception_comma.py:

try:
    result = 1/0
except Exception, e:
    print("An error occurred")
    exit(1)

exit(0)
  • $ python --version --> Python 2.7.10
$ python exception_comma.py
An error occurred
  • $ python3 --version --> Python 3.7.2
$ python3 exception_comma.py
  File "exception_comma.py", line 3
    except Exception, e:
                    ^
SyntaxError: invalid syntax
Marcello Romani
  • 2,967
  • 31
  • 40