Scope of caught exception instance in Python 2 and 3

Viewed 434

Since in Python variables are accessible outside of their loops and try-except blocks, I naively thought that this code snippet below would work fine because e would be accessible:

try:
    int('s')
except ValueError as e:
    pass
print(e)

In Python 2 (2.7 tested), it does work as I expected and the output is:

invalid literal for int() with base 10: 's'

However, in Python 3 I was surprised that the output is:

NameError: name 'e' is not defined

Why is this?

1 Answers
Related