Encapsulate the try/except blocks inside the while loop (not the other way around):
while True:
try:
A()
except NewConnectionError as err:
# This will also print the reason the exception occurred
print ('Detected error: {}'.format(err))
else:
print("A() returned successfully.")
finally:
print ("Next loop iteration...")
You can safely omit the else
and finally
blocks. I have only included them for illustrative purposes.
else
is only executed if an exception does NOT occur (that is, if the statements in the try block were successful).
finally
is always executed regardless whether an exception occurs or not.
0
solved Python3 error handling