This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: UnboundLocalError error while handling exception
Type: behavior Stage:
Components: Versions: Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: socketpair, vstinner
Priority: normal Keywords:

Created on 2016-01-29 09:40 by socketpair, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg259201 - (view) Author: Марк Коренберг (socketpair) * Date: 2016-01-29 09:40
This works right in Python 2.7, but fails in python3:

UnboundLocalError: local variable 'e' referenced before assignment


def test():
    try:
        raise Exception('a')
    except Exception as e:
        pass
    else:
        return
    print(e)

test()
msg259202 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-29 09:42
Yes, it's a deliberate change in the Python language. You have to copy "e" to a different variable:

err = None
try:
  ...
except Exception as exc:
  err = exc
print(err)
History
Date User Action Args
2022-04-11 14:58:27adminsetgithub: 70425
2016-01-29 09:42:33vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg259202

resolution: not a bug
2016-01-29 09:40:26socketpaircreate