-1

I have this python selenium script that after few minutes always throws some kind of error. Usually because chrome runs out of memory or there is some problem with proxy, but other errors also so its hard to catch them all. More simple for me would be solution, that the script would just restart itself every time there is an error. I know how to restart script, I just don't know how to tell python to do it when ANY error happens. Another solution would be something like "error ignore" because my script is already set to restart itself every x loops, but I cannot find anything like that for python.

4
  • 1
    You could catch every kind of error by just using a huge try and except around your whole code, and then loop the function in your except statement. Would that work for you? Commented Jun 13, 2017 at 8:23
  • Thanks nostradamus, this worked for me. Commented Jun 18, 2017 at 19:08
  • 1
    I turned the comment into an answer. It would be nice if you could accept it / close the question. Thanks. Commented Jun 19, 2017 at 8:13
  • stackoverflow.com/a/33334183/1340631 has a nice solution. Commented Jan 6, 2021 at 10:42

1 Answer 1

1

You could catch every kind of error by just using a try/except around your whole code, and then restart the function in your except statement when any kind of error occurs. Here is a snippet of pseudo code:

def myfunc():
    try:
        do_something
    except:   # or catch one specific error with 'except AttributeError:'
        myfunc()
Sign up to request clarification or add additional context in comments.

3 Comments

This leads to a recursion and will eventually reach the maximum recursion depth. Also it doesn't clean up anything that happens inside myfunc().
Agreed, it's nasty. But how would you solve the problem in an elegant way? In theory, automatically restarting a script after an exception occured should never be necessary in the first place.
stackoverflow.com/a/33334183/1340631 has a solution without this recursion problem. It also closes opened files and connections on restart. Not sure if there are any other problems, though. And I agree with you that automatic restarts can be problematic, especially if there is no proper cleanup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.