Python - Try/Resume -


i looking way resume code after handling exception. specifically, experiencing issue trying click different buttons, pop-up appears , interrupts process. cannot predict when pop-up occur, whenever does, need continue left off. way can think of coding 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, impractical way of coding this, since every single line have in own try/except block, can't seem find better.

edit: don't worry, everybody, there specific exceptions in use of :) used except: simplicity's sake.

if there several functions have handle same kind of exception , deal same way, write decorator , decorate 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 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, use them:

click_button_1() click_button_2() click_button_3() 

edit: catching exception many times

sam asked in comments solution using decorator, handling multiple exceptions (as in alex's answer).

it easy 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, may or may not desired, depending on situation. furthermore, rather not catch generic exception. instead of that, specific exception expected should handled way.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -