I am confused about the exception handling in python3.
Say that I want to catch an exception and store it for later use; why doesn't the following code work? (it raises a NameError: name 'e' is not defined)
try:
[][0]
except IndexError as e:
pass
e
Why is e treated as a local variable within the try block?
As a way around, I realized that I could "reassign" it; namely, the following code works:
try:
[][0]
except IndexError as e:
z = e
z
But then, why does the following still not work?
try:
[][0]
except IndexError as e:
e = e
e
The code above still raises a NameError: name 'e' is not defined; but shouldn't e = e behave exactly the same as z = e?
Then again, I did find a solution to use the exception as I want, but I would appreciate your help in understanding why the other two attempts fail. Thank you in advance.