Home > Mobile >  Retry after exception in delphi
Retry after exception in delphi

Time:09-30

I have a question for you.

I have a piece of code as follows.

try
 //some code that fails
except
 // code to retry the code that fails
end 

Now I want to retry the failing code after the exception. Is it possible to do that in Delphi? So you have a kind of loop that retries after an exception for 3/4 times. and if it didn't work at the 4th time then give an error message.

CodePudding user response:

I often use this construct:

FOR I:=1 TO Retries DO BEGIN
  TRY
    <Code>
    BREAK
  EXCEPT
    <Report/Log failure, prepare for next iteration>
  END
END

this way, it loops around "Retries" number of times, but if it succeeds at some point, it breaks out of the loop.

The EXCEPT part should prepare for the next iteration of the retry loop (like delete any files created by the failed code, etc.), perhaps guarded by an

IF I=Retries THEN
  RAISE
ELSE BEGIN
  <Report/Log failure, prepare for next iteration>
END
  • Related