python catch exception and continue try block

前端 未结 9 2426
眼角桃花
眼角桃花 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:28

    I don't think you want to do this. The correct way to use a try statement in general is as precisely as possible. I think it would be better to do:

    try:
        do_smth1()
    except Stmnh1Exception:
        # handle Stmnh1Exception
    
    try:
        do_smth2()
    except Stmnh2Exception:
        # handle Stmnh2Exception
    

提交回复
热议问题