I have written this function to use it into another class, but code generates an error and says, Not all code paths return a value. I also tried to search the solution regarding this problem and most of the answers were irrelevant to the actual problem. Hoping to bump into the logical understanding for this kind of same errors.
using System;
namespace Practice
{
class ContinueAgain
{
public int Continue() // (Not all code paths return a value)
{
rerun:
try {
char chAgain;
// user guiding message and then taking input
Console.WriteLine("\nAgain? (y/n)\n");
chAgain = char.Parse(Console.ReadLine());
// filtering the condition if the user wants to continue or not
// exiting out of the method if user selects 'Y'/'y'
if (chAgain == 'y' || chAgain == 'Y') // if block begins
{
return 1;
}
// exiting out of the method if user selects 'N'/'n'
else if (chAgain == 'n' || chAgain == 'N')
{
Console.WriteLine("\nThank you for using :)\n");
return 0;
}
// printing user message if user enters any input apart from the asked input
if(chAgain!='y' || chAgain!='Y' || chAgain!='n' || chAgain!='N')
{
Console.WriteLine("\nThis input in invalid\n");
goto rerun;
}
#endregion if block 4 - ends
} // try block ends
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Would like to know what's causing or triggering this error event.
CodePudding user response:
You need to return a value inside the catch
block and at the end of the try
block.
CodePudding user response:
There are 2 missing return statements in your code - after the last if
statement and in the catch
block