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