Difference Between except Exception as e and except Exception e in Python



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





Updated on: 2020-06-25T20:57:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements