简体   繁体   English

delphi 异常后重试

[英]Retry after exception in delphi

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?是否可以在 Delphi 中执行此操作? So you have a kind of loop that retries after an exception for 3/4 times.所以你有一种在异常发生后重试 3/4 次的循环。 and if it didn't work at the 4th time then give an error message.如果它在第 4 次不起作用,则给出错误消息。

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 EXCEPT 部分应该为重试循环的下一次迭代做准备(比如删除由失败代码创建的任何文件等),也许由一个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM