Return part of result if occur a except
<p>I am using python. I have a function (getAll) that call other function in a loop (getPart) and in each step the return value is updated. In some case when I call the function that is inside the loop, this fail. I need return the result in this moment.</p>

<pre><code>def getAll(m, d, v, t, s, tn, type):
  result = []
  flag = 0
  while flag == 0:
    tempResult = getPart(m, d, v)
    for i in range(0, len(tempResult)):
      result.append(tempResult[i])
    flag = tempResult[0]
  return result

print getAll(5,4,1,'ds',8,'data')
</code></pre>

<p>I need print the result partial value,  if occur a except in some step when I call tempResult in getAll</p>

<p>You can handle the exception by wrapping the code that raises the error with a <code>try/except</code> and <em>printing</em> the result in the <code>except</code> block:</p>

<pre><code>def getAll(m, d, v, t, s, tn, type):
    result = []
    flag = 0
    while flag == 0:
        try:
            tempResult = getPart(m, d, v)
        except SomeError: # specify error type
            print('The partial result is', result)
            raise  # re-raise error
        for i in range(0, len(tempResult)):
            result.append(tempResult[i])
        flag = tempResult[0]
    return result

print getAll(5,4,1,'ds',8,'data')
</code></pre>

<p>On another note, since you already know calling <code>getPart</code> might raise an error, you may move the <code>try/except</code> block into the function. This depends of course on what exactly you're trying to achieve.</p>

python except