When writing try/except statements, whether I use
except KeyError:
or
except KeyError as e:
I get the same result.
What is the difference between the two? Is KeyError as e
just more specific/custom?
When writing try/except statements, whether I use
except KeyError:
or
except KeyError as e:
I get the same result.
What is the difference between the two? Is KeyError as e
just more specific/custom?
When using except KeyError as e:
, you can access the exception and its attributes as an object with e
. Like so:
def test_function():
try:
do_something_that_fails()
except Exception as e:
print e.message, e.args
It will help with debugging any problems you have when the exception is thrown.
You can find more information on how this works in the Python Documentation.
Good luck!