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. code c #if c fails, go to d code d except: pass Is something like this possible?
49 Answers
You'll have to make this separate try blocks:
try: code a except ExplicitException: pass try: code b except ExplicitException: try: code c except ExplicitException: try: code d except ExplicitException: pass This assumes you want to run code c only if code b failed.
If you need to run code c regardless, you need to put the try blocks one after the other:
try: code a except ExplicitException: pass try: code b except ExplicitException: pass try: code c except ExplicitException: pass try: code d except ExplicitException: pass I'm using except ExplicitException here because it is never a good practice to blindly ignore all exceptions. You'll be ignoring MemoryError, KeyboardInterrupt and SystemExit as well otherwise, which you normally do not want to ignore or intercept without some kind of re-raise or conscious reason for handling those.
You can use fuckit module.
Wrap your code in a function with @fuckit decorator:
@fuckit def func(): code a code b #if b fails, it should ignore, and go to c. code c #if c fails, go to d code d 4Extract (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 6If you don't want to chain (a huge number of) try-except clauses, you may try your codes in a loop and break upon 1st success.
Example with codes which can be put into functions:
for code in ( lambda: a / b, lambda: a / (b + 1), lambda: a / (b + 2), ): try: print(code()) except Exception as ev: continue break else: print("it failed: %s" % ev) Example with arbitrary codes (statements) directly in the current scope:
for i in 2, 1, 0: try: if i == 2: print(a / b) elif i == 1: print(a / (b + 1)) elif i == 0: print(a / (b + 2)) break except Exception as ev: if i: continue print("it failed: %s" % ev) Lets say each code is a function and its already written then the following can be used to iter through your coding list and exit the for-loop when a function is executed without error using the "break".
def a(): code a def b(): code b def c(): code c def d(): code d for func in [a, b, c, d]: # change list order to change execution order. try: func() break except Exception as err: print (err) continue I used "Exception " here so you can see any error printed. Turn-off the print if you know what to expect and you're not caring (e.g. in case the code returns two or three list items (i,j = msg.split('.')).
1You could try a for loop
for func,args,kwargs in zip([a,b,c,d], [args_a,args_b,args_c,args_d], [kw_a,kw_b,kw_c,kw_d]): try: func(*args, **kwargs) break except: pass This way you can loop as many functions as you want without making the code look ugly
I use a different way, with a new variable:
continue_execution = True try: command1 continue_execution = False except: pass if continue_execution: try: command2 except: command3 to add more commands you just have to add more expressions like this:
try: commandn continue_execution = False except: pass I ran into this problem, but then it was doing the things in a loop which turned it into a simple case of issueing the continue command if successful. I think one could reuse that technique if not in a loop, at least in some cases:
while True: try: code_a break except: pass try: code_b break except: pass etc raise NothingSuccessfulError 0Like Elazar suggested: "I think a decorator would fit here."
# decorator def test(func): def inner(*args, **kwargs): try: func(*args, **kwargs) except: pass return inner # code blocks as functions @test def code_a(x): print(1/x) @test def code_b(x): print(1/x) @test def code_c(x): print(1/x) @test def code_d(x): print(1/x) # call functions code_a(0) code_b(1) code_c(0) code_c(4) output:
1.0 0.25