我有以下(简化)方法:
public bool DoWorkWithRetry()
{
for (int remainingTries = Constants.MaxRetries; remainingTries >= 0; remainingTries--)
{
try
{
return DoWork();
}
catch (Exception ex)
{
if (remainingTries == 0)
{
throw new WorkException(
String.Format("Failed after {0} retries.", Constants.MaxRetries),
ex);
}
// fall through to retry
}
}
}
对我来说似乎很清楚这个方法要么返回要么抛出。但是,C# 编译器向我抱怨not all code paths return a value
.
- 这是 C# 编译器代码分析的限制吗?
- 或者是否有某种情况我没有看到
for
循环可以在没有抛出或返回的情况下完成?