One of the easier ways is to ignore them and continue moving through the loop. This is accomplished with the try function which simply wraps around the entire body of the loop. By default, try will continue the loop even if there's an error, but will still show the error message.
You can do the same type of for loop if you want to loop over every character in a string. To loop through a set of code a certain number of times, you can use the range() function, which returns a list of numbers starting from 0 to the specified end number.
A "For" Loop is used to repeat a specific block of code a known number of times.
Is there a command such as break
and continue
which could repeat recent iteration?
For example, when exception is thrown.
for i in range(0,500):
try:
conn = getConnection(url+str(i))
doSomething(conn)
except:
repeat
Let's have an iteration where i
variable's value is 6
. During this iteration some connection error occurred. I want to repeat this iteration.
Is there a command which can do that?
Of course I can do this:
i=0
while i!=500:
try:
conn = getConnection(url+str(i))
doSomething(conn)
i+=1
except:
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With