views:

598

answers:

6

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;


        }
+4  A: 

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.

Henk Holterman
so where should I handle condition for null and empty strings?
Learner
@Learner: I answered that below
280Z28
Learner, Revers checks it argument (OK) but it should not handle (consume) that error by it self. It is a signal to the code (caller) that supplied the bad argument. And where does Console.Writeline go in an ASP.NET or Windows program? Reverse should not assume anything about the application type.
Henk Holterman
+2  A: 

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.

Alex Martelli
+4  A: 

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);
280Z28
The try block does not make it slower.
Henk Holterman
It makes it less likely for the X64 JIT to produce fully optimal code from it. (can't find the link I was after and I have to run to see HP!)
280Z28
+1, Using `Array.Reverse` is cleaner *and* faster than any of the other suggestions. (Well, it was quicker in a handful of quick-and-dirty benchmarks that I just tried.)
LukeH
+2  A: 

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

GBegen
The easiest way to do this is add a "throw;" line after your Console.WriteLine.
Jacob
+1  A: 

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
}
Timothy Carter
I would return String.Empty, there is nothing principally wrong with asking for the reverse of an empty string. You might want to treat null differently though.
Henk Holterman
+1  A: 

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;
}
Michael Ciba
+1, Using `Array.Reverse` is cleaner *and* faster than any of the other suggestions. (Well, it was quicker in a handful of quick-and-dirty benchmarks that I just tried.)
LukeH