access to original dict after a KeyError

78 views Asked by At

If I catch an KeyError exception in python, I can easily get the key that was failed. Is there a way to access the keys that are in the dictionary? I know the exception itself doesn't have the information, but is there a way to find it from the stack trace?

1

There are 1 answers

2
Martijn Pieters On BEST ANSWER

No, the exception doesn't retain a reference to the dictionary that threw the exception. As such, you cannot enumerate the keys that do exist from just the exception.