On Error Resume Next in Python

后端 未结 7 1159
栀梦
栀梦 2020-12-11 02:07

Snippet 1

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

Snippet 2

try:
    do_mag         


        
7条回答
  •  感情败类
    2020-12-11 02:24

    If you are the one coding the fucntions, why not program the functions to return status codes? Then they will be atomic and you wont have to capture the error in the main section. You will also be able to perform roll back or alternate coding on failure.

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

    in 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...
    

提交回复
热议问题