Given the following code:
msg = "test"
try:
"a"[1]
except IndexError as msg:
print("Error happened")
print(msg)
Can somebody explain why this causes the following output in Python 3?
Error happened
Traceback (most recent call last):
File "test.py", line 6, in <module>
print(msg)
NameError: name 'msg' is not defined
msg
gets deleted after theexcept:
, including reference to the docs, so IMHO it should be the accepted answer. – Corbett