简体   繁体   English

循环内的异常处理。 为什么此代码给出的不是所有代码路径都返回值错误?

[英]Exception handling within loop. Why does this code give a not all code paths return a value error?

public static Boolean CanParseStringToInt(String s)
    {
        Boolean retry = false;
        int n = 0;

        do
        {
            try
            {
                n = int.Parse(s);
                return true;
            }
            catch (Exception ex)
            {
                if (!retry)
                {
                    retry = true;
                }
                else
                {
                    return false;
                }
            }
        }
        while (retry);
    }

I'm targeting .Net 4.0 and using VS2012. 我的目标是.Net 4.0并使用VS2012。 The code is just an example. 该代码仅是示例。

The compiler can't absolutely tell if a piece of code will eventually return a value. 编译器无法绝对确定一段代码是否最终将返回一个值。 Look at the Halting problem for an example of the difficulty. 查看暂停问题 ,以了解难度示例。

If you restructure the code like this, it should behave the same way but will fix the error: 如果您以这种方式重组代码,则其行为应相同,但将修复错误:

public static Boolean CanParseStringToInt(String s)
{
    Boolean retry = false;
    int n = 0;

    do
    {
        try
        {
            n = int.Parse(s);
            return true;
        }
        catch
        {
            if (!retry)
            {
                retry = true;
            }
            else
            {
                break;
            }
        }
    }
    while (retry);

    return false;
}

The compiler is not smart enough to know that you will eventually return inside the loop. 编译器不够聪明,无法知道您最终将在循环内返回。

I would add this line at the end of the method: 我将在方法末尾添加以下行:

throw new InvalidOperationException("Should not have reached here.");

You are not returning anything after your while loop. while循环之后,您什么也不返回。

Not sure what this method is used for, but I would suggest Int32.TryParse() instead of this implementation. 不知道此方法用于什么,但是我建议使用Int32.TryParse()代替此实现。

There is no return statement in the if block inside the catch block. catch块内的if块中没有return语句。 Hence the body of the loop is assumed to execute. 因此,假定循环体已执行。 This means the while condition also executes and hence the compiler assumes it can return false. 这意味着while条件也会执行,因此编译器认为它可以返回false。 This means there must also be a return following the while statement 这意味着在while语句之后还必须有一个返回值

In your catch the else condition returns but the if does not, it instead does assignment. 在您的捕获中,else条件返回,但if不返回,而是分配。 There's also no return outside of your while. 这段时间之外也没有任何回报。

       catch (Exception ex)
        {
            if (!retry)
            {
                retry = true;
            }
            else
            {
                return false;
            }
        }

In general I prefer to write retry logic like below; 通常,我喜欢编写如下的重试逻辑;

 for (int i = 0; i < retries; i++)
 {
     try
     {
         //do stuff that could throw
     }
     catch (Exception e)
     {
         Thread.Sleep(500);
         if (i == retries - 1)
         {
             throw new Exception(e.Message);
         }
     }
  }

Also, after a second look at your code it seems like you're re writing int.TryParse(string input, out int output) 另外,再次查看代码后,似乎您正在写int.TryParse(string input, out int output)

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

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