python catch exception and continue try block

前端 未结 9 2458
眼角桃花
眼角桃花 2020-11-29 00:59

Can I return to executing try-block after exception occurs? (The goal is to write less) For Example:

try:
    do_smth1()
except:
    pass

try:
    do_smth2(         


        
9条回答
  •  广开言路
    2020-11-29 01:16

    'continue' is allowed within an 'except' or 'finally' only if the try block is in a loop. 'continue' will cause the next iteration of the loop to start.

    So you can try put your two or more functions in a list and use loop to call your function.

    Like this:

    funcs = [f,g]
    for func in funcs:
        try: func()
        except: continue
    

    For full information you can go to this link

提交回复
热议问题