python using recursive try-except-finally in

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011510825/article/details/86360478

    Yesterday found try-except-finally use recursion, the results fell into the pit, climb a long, long time .....

    Demand is so, we request a three-way interface that it sometimes unstable and needs to be retried maximum retry test five times, then the code looks like this.

def request_interfaces(num):
    print("request_interfaces num:%s" % num)
    raise Exception("request_interfaces error")


def exce_function(max_retry=5):
    num = 0
    try:
        request_interfaces(num)
    except:
        num += 1
        print("except num:%s" % num)
        if num < max_retry:
            request_interfaces(num)
    finally:
        print("finally num:%s" % num)
    print("end!!!")

I thought, surely he can retry loop five times, and then exit. . But the fact is that he can only loop twice, that is, once he entered except, then try again encounter raise time, directly executed finally.

Output is as follows:

>>> exce_function()
request_interfaces num:0
except num:1
request_interfaces num:1
finally num:1
Traceback (most recent call last):
  File "<stdin>", line 4, in exce_function
  File "<stdin>", line 3, in request_interfaces
Exception: request_interfaces error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in exce_function
  File "<stdin>", line 3, in request_interfaces
Exception: request_interfaces error

If you feel the above recursive complex, we use the following well-understood this


def exec_function():
    num = 0
    try:
        request_interfaces(0)
    except:
        request_interfaces(1)
        request_interfaces(2)
        request_interfaces(3)
    finally:
        print("finally num:%s" % num)
    print("end!!!")

The result is the same

>>> exec_function()
request_interfaces num:0
request_interfaces num:1
finally num:0
Traceback (most recent call last):
  File "<stdin>", line 4, in exec_function
  File "<stdin>", line 3, in request_interfaces
Exception: request_interfaces error

The problem here, request_interfaces no abnormal capture, exec_function the try-except-finally with request_interfaces absolutely nothing, can be understood as a direct implementation of the request_interfaces function (we easily try-except misleading exce_function, and it would also enter the try-except a) .

 There are many ways changes here, my choices are as follows:

def request_interfaces(num, max_retry=5):
    try:
        print("request_interfaces num:%s" % num)
        raise Exception("request_interfaces error")
    except:
        num += 1
        print("except num:%s" % num)
        if num < max_retry:
            request_interfaces(num, max_retry)
    finally:
        print("finally num:%s" % num)

def exec_function():
    num = 0
    request_interfaces(num)
    print("end!!!")

The results are as follows (as expected):

>>> exec_function()
request_interfaces num:0
except num:1
request_interfaces num:1
except num:2
request_interfaces num:2
except num:3
request_interfaces num:3
except num:4
request_interfaces num:4
except num:5
finally num:5
finally num:4
finally num:3
finally num:2
finally num:1
end!!!

 

Guess you like

Origin blog.csdn.net/u011510825/article/details/86360478