I have a try/except block around a call to an API. It appears to me that once I get an exception, all valid try cases after that exception will see the same exception. The only way I have been able to get it to work is to re-start my Python script. I googled around and found PyErr_clear() but that is for the C-API. Is there something I can call from plain-old Python that will clear the exception state?
Here is the basic idea of what I am doing:
def get_and_print_data(key):
try:
data = get_data_from_some_3rd_party_api(key)
except Exception as ex:
print("Failed to get data for",key,": ",str(ex))
return
print("data=",data)
Then in main I have
get_and_print_data("Valid Key") ## This works
get_and_print_data("INvalid Key") ## This gets exception, as expected
get_and_print_data("Valid Key") ## This and all subsequent calls to get_and_print_data() fail with the same exception.
get_data_from_some_3rd_party_api(key)failing after receiving an invalid key. I don't expect that behavior from your try/except.