Sam Krygsheld
Sam Krygsheld

Reputation: 2060

Python - Try/Resume

I am looking for a way to resume code from where it was after handling an exception. Specifically, I am experiencing an issue where I am trying to click different buttons, but sometimes a pop-up appears and interrupts the process. I cannot predict when the pop-up will occur, but whenever it does, I need to continue where I left off. The only way I can think of coding this is something like this:

try:
    click_button_1()
except:
    handle_pop_up()
    click_button_1()

try:
    click_button_2()
except:
    handle_pop_up()
    click_button_2()

try:
    click_button_3()
except:
    handle_pop_up()
    click_button_3()

Obviously, this is a very impractical way of coding this, since every single line would have to be in its own try/except block, but I can't seem to find anything better.

Edit: Don't worry, everybody, there will be specific exceptions in my use of this :) I just used except: for simplicity's sake.

Upvotes: 0

Views: 289

Answers (3)

zvone
zvone

Reputation: 19352

If there are several functions which have to handle the same kind of exception and deal with it the same way, I would write a decorator and decorate all of them:

def with_exception_handling(func):
    def handled_func(*a, **kw):
        try:
            func(*a, **kw)
        except Exception:
            handle_pop_up()
            func(*a, **kw)

And then decorate each of them:

@with_exception_handling
def click_button_1():
    ...

@with_exception_handling
def click_button_2():
    ...

@with_exception_handling
def click_button_3():
    ...

Later, just use them:

click_button_1()
click_button_2()
click_button_3()

EDIT: catching the exception many times

Sam asked in comments for a solution using a decorator, but handling multiple exceptions (as in Alex's answer).

It is easy to do both:

def with_multiple_exception_handling(func):
    def handled_func(*a, **kw):
        while True:
            try:
                func(*a, **kw)
            except Exception:
                handle_pop_up()
            else:
                break

Disclaimer: Of course, this may or may not be desired, depending on the situation. Furthermore, I would rather not catch the generic Exception. Instead of that, the specific exception which is expected should be handled this way.

Upvotes: 1

Illusionist
Illusionist

Reputation: 5489

You can use the retry decorator.

from retrying import retry
def my_exception(exception):
    #should really catch particular exceptions, implement the next 
    #if isinstance(exception,yourexceptiontype)
    print "Saw an exception"

@retry(retry_on_exception=my_exception, hook=handle_pop_up)
def run():

     click_button_1()
     click_button_2() #and continue

Upvotes: 0

Alex Hall
Alex Hall

Reputation: 36013

for func in [click_button_1,
             click_button_2,
             click_button_3]:
    while True:
        try:
            func()
        except:  # you should really put a specific exception here
            handle_pop_up()
        else:
            break

The while loop is to ensure proper behaviour even if the problem happens many times.

Upvotes: 1

Related Questions