E
E
Evgeny Ivanov2016-11-08 07:29:38
Delphi
Evgeny Ivanov, 2016-11-08 07:29:38

How to execute code until successful result (try except)?

There is code that requests the content of the page (sockets, IdHTTP, https, etc.) and also performs other actions. Unfortunately, the page may not always be available due to server load, unstable Internet connection, etc. As a result, the program works through time. Through no fault of their own.
How to execute the code in the program until a successful result? Those. if the first time an error occurs - try to execute the code again in half a second. And so, say, five times?
And if it didn't work out for the fifth time, go to another block of code (display a message, etc.)
I tried try except with a counter and label (transitions).
By type if error counter+1 and go to the beginning of the code.
If counter=5 then stop and display message.
But it is impossible to jump to the beginning of the code in this construction.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2016-11-08
@alexey-m-ukolov

Tried try except with counter and label (transitions).
By type if error counter+1 and go to the beginning of the code.
If counter=5 then stop and display message.
But it is impossible to jump to the beginning of the code in this construction.
You need recursion - the function will call itself in case of an error.

D
dude2012, 2016-11-08
@dude2012

Can be done as follows. Each step is formalized as a procedure and labeled (step1, step2, step3). Above all these steps there is a
<Main loop> loop ;
if step 1 is successful, continue the loop
; if not, then continue with a marker that the loop ended at the 1st step.
if step 2 is successful, continue the loop
; if not, then continue with a marker that the loop ended at the 2nd step.
......
if step N is successful, continue the loop;
if not, then continue with a marker that the loop ended at the Nth step.
All actions completed, break.
End of cycle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question