Csharp/C#教程:没有goto 编写重试逻辑的更好方法分享


没有goto 编写重试逻辑的更好方法

有没有更好的方法来编写这个代码而不使用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; } } 

这是我将使用的基本逻辑,而不是goto语句:

 bool succeeded = false; int tries = 2; do { try { m_Outputfile = new StreamWriter(m_Filepath, true); m_Outputfile.Write(body); m_Outputfile.Flush(); succeeded = true; } catch(Exception) { tries--; } } while (!succeeded && tries > 0); 

我刚刚添加了尝试逻辑,即使原始问题没有。

Michael的解决方案并不能完全满足要求,即重试固定次数,抛出最后一次失败。

为此,我建议一个简单的for循环,倒计时。 如果您成功,请退出rest(或者,如果方便的话,返回)。 否则,让catch检查索引是否为0。如果是,请重新抛出而不是记录或忽略。

 public void Write(string body, bool retryOnError) { for (int tries = MaxRetries; tries >= 0; tries--) { try { _outputfile.Write(body); _outputfile.Flush(); break; } catch (Exception) { if (tries == 0) throw; _outputfile.Close(); _outputfile = new StreamWriter(_filepath, true); } } } 

在上面的例子中,返回会很好,但我想展示一般情况。

@ Michael的回答 (使用正确实现的catch块)可能是最容易使用的,并且是最简单的。 但是为了呈现替代方案,这里有一个版本将“重试”流控制分解为一个单独的方法:

 // define a flow control method that performs an action, with an optional retry public static void WithRetry( Action action, Action recovery ) { try { action(); } catch (Exception) { recovery(); action(); } } public void Send(string body) { WithRetry(() => // action logic: { m_Outputfile.Write(body); m_Outputfile.Flush(); }, // retry logic: () => { m_Outputfile = new StreamWriter(m_Filepath, true); }); } 

当然,您可以通过重试计数,更好的错误传播等方法来改进这一点。

如果你把它放在一个循环中怎么办? 也许是类似的东西。

 while(tryToOpenFile) { try { //some code } catch { } finally { //set tryToOpenFile to false when you need to break } } 

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

尝试以下内容:

 int tryCount = 0; bool succeeded = false; while(!succeeded && tryCount<2){ tryCount++; try{ //interesting stuff here that may fail. succeeded=true; } catch { } } 

用布尔值

上述就是C#学习教程:没有goto 编写重试逻辑的更好方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public void Write(string body) { bool NotFailedOnce = true; while (true) { try { _outputfile.Write(body); _outputfile.Flush(); return; } catch (Exception) { NotFailedOnce = !NotFailedOnce; if (NotFailedOnce) { throw; } else { m_Outputfile = new StreamWriter(m_Filepath, true); } } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/954626.html

(0)
上一篇 2021年11月20日
下一篇 2021年11月20日

精彩推荐