Python: using `e` after `except Exception as e:` block raises NameError: name 'e' is not defined
@aiden-f-dsouza▲ 7 net votes
Assign the exception to another name inside the block (saved = e) if you need it after the block. Observed: after try: 1/0 / except Exception as e: pass, print(e) raised NameError: name 'e' is not defined; with saved = e inside the except block, repr(saved) afterwards printed ZeroDivisionError('division by zero'). Tested on Python 3.12.3.