getting error not all code paths return value by c# compiler

牧云@^-^@ 提交于 2020-01-04 05:09:15

问题


This is a basic string reverse program and I want to do some level of exception handling in it. But during compilation it gives me an error "NOt all code paths return value. I am not able to find out why

 public static string Reverse(string s)
        {
            try
            {
                if (string.IsNullOrEmpty(s))
                {
                    throw new NullReferenceException();
                }

                char[] c = s.ToCharArray();
                int start = 0;
                int end = c.Length - 1;
                char temp;

                while (start < end)
                {
                    temp = c[start];
                    c[start] = c[end];
                    c[end] = temp;
                    start++;
                    end--;
                }
                return new string(c);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Thanks guys...I change the code to something like this

 public static string Reverse(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                char[] c = s.ToCharArray();
                int start = 0;
                int end = c.Length - 1;
                char temp;

                while (start < end)
                {
                    temp = c[start];
                    c[start] = c[end];
                    c[end] = temp;
                    start++;
                    end--;
                }
                return new string(c);
            }
            else return s;


        }

回答1:


If an exception happens then there is no return statement being executed. Walk it through.

The best remedy (my choice) would be to remove the entire try/catch . A utility function like Reverse should not handle (its own) exceptions.




回答2:


If you throw an exception before the return statement, the catch handler is called. After the catch handler executes, it procedes past it (since there's no return or throw statement in it), at which point it reaches the end of the method without returning a value.

Edit 2 (major bug): You throw an ArgumentNullException, and procede to catch it and "eat" it, so it's pointless (in this form). You should do your parameter validation before entering a try block, plus this method shouldn't use a try block at all (it'll make it slower for no useful reason).

Edit: on a side note:

char[] characters = s.ToCharArray();
Array.Reverse(characters);
return new string(characters);



回答3:


You have to return a string from the catch clause as well as from the try clause (either that, or raise some exception) -- right now you don't have a return in your catch clause.




回答4:


In your catch block you either need to return a string or throw an exception.




回答5:


I think the real question is how do you want to handle an inputted null or empty string. If you believe your method should handle this by silently "correcting", you can return String.Empty. If however, you believe the calling methods should deal with this error, then throwing an exception and not catching it seems like the appropriate course of action. Regardless of which you choose it seems you shouldn't need the try/catch block.

public static string Reverse(string s)
{
     if (String.IsNullOrEmpty(s))
     {
          //option 1
          return String.Empty;
          //option 2
          throw new NullReferenceException();
     }
     //rest of method
}



回答6:


A cleaner way of doing it might be as follows

static void Main(string[] args)
{
    string reverseMe = "hello world";
    string reversed = ReverseString(reverseMe);
    Console.WriteLine(reversed);
}

private static string ReverseString(string reverseMe)
{
    if (String.IsNullOrEmpty(reverseMe)) return String.Empty;
    char[] reverseMeArray = reverseMe.ToCharArray();
    Array.Reverse(reverseMeArray);
    string result = new string(reverseMeArray);
    return result;
}


来源:https://stackoverflow.com/questions/1148218/getting-error-not-all-code-paths-return-value-by-c-sharp-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!