How to change proxy if it fails

There is two ways to implement that. The easiest way is to set fail number. If proxy will die or fail to load page, current thread will fail and fail number will increase. If actual fail number will be greater than allowed fail number whole script will finish, if not, thread will immediately restart and change proxy.

See this video tutorial:

If you want to set proxy without interrupting current thread, you need to put actions that may fail into a function. If proxy will fail during function execution, thread won't exit.