Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

What is the difference between 'except Exception as e' and 'except Exception, e' in Python?

There are two similar codes as follows.

try:
pass
except Exception, e:
pass

and:

try:
pass
except Exception as e:
pass

I would like to know if there is any change in functionality with the change in syntax.


1 Answer
Rajendra Dharmkar

The difference between using ',' and 'as' in except statements, is as follows:

Both ',' and 'as' are same functionality wise; but their use depends on the python versions as follows.
In Python 2.5 and earlier versions, use of the 'comma' is recommended since 'as' isn't supported.
In Python 2.6+ versions, both 'comma' and 'as' can be used. But from Python 3.x, 'as' is required to assign an exception to a variable.
As of Python 2.6 using 'as' allows us an elegant way to catch multiple exceptions in a single except block as shown below
except (Exception1, Exception2) as err
is any day better than
except (Exception1, Exception2), err


Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.