Today I learnt why pdb can't access an exception ๐Ÿ

I like to use pdb for debugging in Python, Iโ€™d noticed when using it to debug exceptions I canโ€™t always access the exception. Take the following code:

import pdb
try:
    1/0
except Exception as err:
    pdb.set_trace()

If you run it and try and inspect the value of err youโ€™ll see *** NameError: name 'err' is not defined. Why is this? ๐Ÿค”

The exception is only within scope in the try except block, if the call to pdb.set_trace() is the last line then the block has been closed when we enter the debugger.

A simple way to get round this is to add a pass after the pdb call.

import pdb
try:
    1/0
except Exception as err:
    pdb.set_trace()
    pass

Thanks to Stack Overflow for teaching me this! https://stackoverflow.com/questions/38672560/why-cant-pdb-access-a-variable-containing-an-exception
https://stackoverflow.com/questions/62796591/breakpoint-in-except-clause-doesnt-have-access-to-the-bound-exception