Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python try / except keep trying until no errors

I have the following code that occasionally crashes due to a permissions bug. I am trying to wrap it up in a try / except statement that will keep trying to launch the driver until successful...

def init_driver():
    ffprofile = webdriver.FirefoxProfile("my_profile")
    ffprofile.add_extension(extension="myaddon.xpi")
    return driver

driver = init_driver()

I have seen examples letting me print a message if an error occurs but how do I get it to keep retrying? Does anybody have an example they can point me at?

like image 834
fightstarr20 Avatar asked Nov 29 '22 01:11

fightstarr20


1 Answers

Here's a loop that iterates over attempts:

while True:
    try:
        driver = init_driver()
        break
    except Foo:
        continue

Note that this is not a bare except clause. Bare excepts are dangerous because they can capture things like NameError that are so rarely meant to be caught. You should put the specific exception you expect to catch here.

like image 186
Brian Cain Avatar answered Dec 04 '22 13:12

Brian Cain