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;
}