Multiple try codes in one block

后端 未结 6 1473
情书的邮戳
情书的邮戳 2020-12-04 12:59

I have a problem with my code in the try block. To make it easy this is my code:

try:
    code a
    code b #if b fails, it should ignore, and go to c.
    c         


        
6条回答
  •  时光说笑
    2020-12-04 13:38

    Extract (refactor) your statements. And use the magic of and and or to decide when to short-circuit.

    def a():
        try: # a code
        except: pass # or raise
        else: return True
    
    def b():
        try: # b code
        except: pass # or raise
        else: return True
    
    def c():
        try: # c code
        except: pass # or raise
        else: return True
    
    def d():
        try: # d code
        except: pass # or raise
        else: return True
    
    def main():   
        try:
            a() and b() or c() or d()
        except:
            pass
    

提交回复
热议问题