A while back at work I was working with Oracle’s Data Provider for .NET and ran into a really annoying feature. Oracle, in all their wisdom, had chosen not to implement Timeout for queries and even wiser had chosen not to document this. They also chose not to throw a NotImplementedException when attempting to set the Timeout. So after about 2 weeks of messing around with it I finally found out why queries I was setting to run for 30 seconds where taking 10 minutes.
Anywho, besides the point.
Assume the code below (C#):
[code]public class ConsoleServices
{
public static int
GetAnswer (string message, string[] possibleAnswers)
{
string answer = string.Empty;
//Change all possible answers to lower case for comparison
for (int i = 0; i < possibleAnswers.Length; i++)
possibleAnswers[i] = possibleAnswers[i].ToLower ();
//While user hasn't entered in a valid answer
//Continue to ask question.
do
{
Console.Clear ();
Console.WriteLine (message);
Console.Write ("( ");
if (possibleAnswers.Length == 1)
{
Console.WriteLine ("{0} )", possibleAnswers[0]);
}
else
{
for (int i = 0; i < possibleAnswers.Length - 1; i++)
{
Console.Write ("{0}, ", possibleAnswers[i]);
}
Console.WriteLine ("{0} )", possibleAnswers[possibleAnswers.Length - 1]);
}
answer = Console.ReadLine ().ToLower ();
} while (!possibleAnswers.Contains (answer));
//Find and return the index which was selected.
for (int i = 0; i < possibleAnswers.Length; i++)
{
if (possibleAnswers[i] == answer)
return i;
}
//I believe unreachable code.
//
//Im not sure whether to throw an exception or returns some arbitrary value,
//I'm going with throwing an exception, because if this line of code is ever
//reached, something really weird happened and -1 doesn't really say what
//needs to be said.
//
//Whats your opinion?
throw new InvalidOperationException ("Invalid Answer");
//return -1;
}
}[/code]
I’m in a [somewhat not at all] similar predicament, where I don’t know whether I want to throw an exception, or continue program execution. I believe the last line of code in the method GetAnswer is unreachable (if its not, please inform me), but assume that it is [by some freak accident], what SHOULD be done and WHY?