This is the basic string reverse program, and I want to do some level of exception handling in it. But at compile time it gives me the error "NOt all code paths return value. I canβt 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 am changing 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; }
If an exception occurs, then the return statement is not executed. Go through it.
The best tool (my choice) is to remove all try / catch. A utility function, such as Reverse, should not handle (its) exceptions.
return, catch. catch ( return throw), , .
catch
return
throw
2 ( ): ArgumentNullException , "", ( ). try, try ( - ).
ArgumentNullException
try
: :
char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters);
catch, try ( , - ) - return catch.
catch , .
, , . , , "", String.Empty. , , , , . , , , try/catch.
public static string Reverse(string s) { if (String.IsNullOrEmpty(s)) { //option 1 return String.Empty; //option 2 throw new NullReferenceException(); } //rest of method }
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; }
Source: https://habr.com/ru/post/1712947/More articles:Can you offer any advanced examples of creating object-oriented software? - pythonCss submit button / href cross browser - csslibrary for analyzing relative dates (such as a Google calendar) in C # - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1712945/any-reason-why-an-aspnet-20-application-with-no-code-would-take-2-minutes-to-load-on-first-run&usg=ALkJrhibjMPK0JG0-NBI4UwNB0zGfVqRFQZSH Case-Insensitive Aliases - linuxhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1712948/disable-tab-access-to-control-in-winforms&usg=ALkJrhjXEGWGRKS2GefN3Tw1nb671aQJQgYAGNI and junior developers - yagniWhen will the new version of flex for windows be available? - flex-lexerAttribute "id" in microformats - htmlHow to get user input to call Ajax.ActionLink for action? - asp.net-mvcAll Articles