gpt4 book ai didi

c# - 不用 goto 编写重试逻辑的更好方法

转载 作者:太空狗 更新时间:2023-10-29 19:50:52 29 4
gpt4 key购买 nike

<分区>

有没有更好的方法可以不使用 goto 来编写这段代码?这看起来很尴尬,但我想不出更好的方法。我需要能够执行一次重试尝试,但我不想复制任何代码。

public void Write(string body)
{
bool retry = false;
RetryPoint:
try
{
m_Outputfile.Write(body);
m_Outputfile.Flush();
}
catch (Exception)
{
if( retry )
throw;
// try to re-open the file...
m_Outputfile = new StreamWriter(m_Filepath, true);
retry = true;
goto RetryPoint;
}
}

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com