Error while resuming in Python

Fragment 1

do_magic() # Throws exception, doesn't execute do_foo and do_bar do_foo() do_bar() 

Fragment 2

 try: do_magic() # Doesn't throw exception, doesn't execute do_foo and do_bar do_foo() do_bar() except: pass 

Fragment 3

 try: do_magic(); except: pass try: do_foo() ; except: pass try: do_bar() ; except: pass 

Is there a way to write code snippet 3 elegantly?

  • If do_magic() fails or not, do_foo() and do_bar() must be executed.
  • If do_foo() fails or not, do_bar() must be executed.

In Basic / Visual Basic / VBS, there is an instruction called On Error Resume Next that does this.

+6
source share
7 answers

In Python 3.4 onwards, you can use contextlib.suppress :

 from contextlib import suppress with suppress(Exception): # or, better, a more specific error (or errors) do_magic() with suppress(Exception): do_foo() with suppress(Exception): do_bar() 

Alternatively fuckit .

+11
source

If all three functions accept the same number of parameters:

 for f in (do_magic, do_foo, do_bar): try: f() except: pass 

Otherwise, complete the call to the lambda function.

 for f in (do_magic, lambda: do_foo(arg1, arg2)): try: f() except: pass 
+8
source

you can try the nested "try" loop; all this may not be as elegant as you might need. "lambda" is also a good way, did not mention, because it was done in the previous answer

edit:

 try: do_magic() finally: try: do_foo() finally: try: do_bar() except: pass 

edit 2:

Well, damn it, this answer again got a few seconds ago: |

0
source

Lots of id but it works

 try: do_magic() finally: try: do_foo() finally: try: do_bar() finally: pass 
0
source

In the question, Snippet 3 does not work, but will work if you do not mind splitting each line into two lines ...

 try: do_magic() except: pass try: do_foo() except: pass try: do_bar() except: pass 

Working example.

 import sys a1 = "No_Arg1" a2 = "No_Arg2" a3 = "No_Arg3" try: a1 = sys.argv[1] except: pass try: a2 = sys.argv[2] except: pass try: a3 = sys.argv[3] except: pass print a1, a2, a3 

.. if you save it in test.py and then at the CMD prompt on Windows just type test.py , it will return No_Arg1 No_Arg2 No_Arg3 because there were no arguments. However, if you specify some arguments, if the type test.py 111 222 will be returned 111 222 No_Arg3 , etc. (Tested - Windows 7, python2.7).

IMHO it is much more elegant than answers to examples of nesting. It also works in exactly the same way as "Error while retrying next", and I use it when translating from VB6. One problem is that try strings cannot contain conditionals. I found that, as a rule, python cannot contain more than one : per line. However, this simply means splitting the statement into 3 lines, etc.

0
source

If there are no parameters ...

 funcs = do_magic, do_foo, do_bar for func in funcs: try: func() except: continue 
0
source

If you use fucntions encoding, why not program functions to return status codes? Then they will be atomic, and you do not have to fix the error in the main section. You will also be able to rollback or alternative coding in the event of a failure.

 def do_magic(): try: #do something here return 1 except: return 0 

in the main program.

 if do_magic() = 0: #do something useful or not... if do_foo() = 0: #do something useful or not... if do_bar() = 0: #do something useful or not... 
0
source

Source: https://habr.com/ru/post/975879/


All Articles