Question

I've come to a lecture on my python for beginners where a certain topic wasn't explained quite well. So what does this e do?

def x(a, b):
    try:
        return a / b

    except ZeroDivisionError, e:
        return 0
Was it helpful?

Solution

The caught exception is assigned to the name e. You can pick any valid Python identifier:

except ZeroDivisionError, caught_exception:

This lets you use the caught exception; perhaps to print the error message, or use attributes of the exception for other purposes. See the Handling Exceptions chapter of the Python tutorial.

The except <Exception>, <name>: syntax has been deprecated in favor of:

except ZeroDivisionError as e:

which is much more readable and avoids the confusion with the syntax to catch multiple exception types:

except (ZeroDivisionError, ValueError):

Since the function you posted otherwise doesn't use e, it can be safely removed altogether:

def x(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return 0

OTHER TIPS

As ZeroDivisionError is not a very good example to explain the purpose of "e" (could be reason why it was not clear to you), I am going to explain with a different error

Exception: In general, when a Python program encounters a situation that it can't continue (buggy code), it raises an exception. An exception is a Python object that represents an error.

On your Python interpreter if you do anything with an undefined variable (or instance), you get an error like this

>>> a = 1
>>> print a
1
>>> print b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> 

NameError is the name of Exception raised

name 'b' is not defined is the additional information for the raised Exception

Let us repeat the same with try and except block

>>> 
>>> 
>>> try:
...     print a
...     print b
... except NameError:
...     print "I got NameError"
... 
1
I got NameError
>>> 

the output is not explicit about variable/instance causing this exception. you may feel, you can handle this if you have access to the additional information provided by interpreter

that is where the optional argument (of except) "e" or whatever you want to call becomes handy

>>> try:
...     print a
...     print b
... except NameError, e:
...     print "I got NameError"
...     print "Addition Information is:", e
...     
... 
1
I got NameError
Addition Information is: name 'b' is not defined
>>>

as pointed out by others, recommend to use the following convention

 except NameError as e

It saves the message generated by the captured error in the name e. Below is a demonstration:

>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>
>>> try:
...     1/0
... except ZeroDivisionError, e:
...     print e
...
integer division or modulo by zero
>>>

You should note two things however. First is that you do not need to use the name e. You can pick whatever variable name you want. Second, the syntax you gave has been deprecated in favor of:

except Exception as e:

Also, I would like to add that your particular function can be rewritten like so:

def x(a, b):
    return a/b if b else 0

Having the try/except block in there is unnecessary and having e is really unnecessary since the function never uses it.

It might be easier to use the new syntax:

except ValueError as e:
    print e 

which reads more clearly -- you're going to give the exception a name, in this case simply e. See this tutorial on error handling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top