Here is one task that we might be doing: scraping some webpages in a for loop. Then some error happens for one of the pages,
and then the whole process breaks, and we didn’t store results to disk yet. So we have to restart the process again.
Why don’t we just retry one of the steps multiple times, we may get it right at the second time?
Here is a function example to return the same task multiple times with try and catch block, before finally raise the error.
import random |
************* try time at 0*************
there is going to be error
division by zero
************* try time at 1*************
there is going to be error
division by zero
************* try time at 2*************
there is going to be error
division by zero
************* try time at 3*************
there is going to be error
division by zero
************* try time at 4*************
no error
0.0