Python - script stopping loop even though there is try/except/finally

Viewed 18

I have a program that connects to a few REST apis and runs continuously. Since it's connecting to outside services, it can temporarily run into several connection-related errors when there's a hiccup on the REST api's side. For now, when I encounter an error, I'd just like to print a message to the console and keep running the loop until the connection problem resolves on the other end.

The general structure is:

while True:
    try:
        main
    except ConnectionError:
        print('ConnectionError encountered')
    finally:
        pass

The problem: When I specify a particular error in the except statement, when the program runs into that error, it never proceeds to the finally statement, which should continue looping the while True statement.

The weirdness: If instead of specifying a particular error, I instead use except Exception: (which I really don't want to have to do), the program continues on to the finally statement and restarts the loop as desired.

The question: Why would the program halt when it encounters a specified error if there is a finally: pass statement?

0 Answers
Related