((我试图找到一个没有运气的类似问题,所以我希望也许有人可以提供解决方案)
我在第一堂课中有一个do while循环,目标是一直循环直到用户如下输入Feet,Meters或Yards:
string convert = "";
do
{
Console.Clear();
Console.WriteLine("What conversion: Feet, Meters, Yards");
try
{
convert = Convert.ToString(Console.ReadLine());
}
catch (Exception)
{
Console.Clear();
Console.WriteLine("Incorrect conversion");
Console.ReadKey();
}
convert = Values.Input(convert);
Console.WriteLine(convert);
Console.ReadKey();
} while (_____); //Trying to loop while arguement is thrown from my other class
Console.WriteLine("Continue on");
Console.ReadLine();
我的单独的Values类输入法:
public static string Input(string input)
{
string convert = input;
if (convert == "Meters")
{
input = "Meters";
}
else if (convert == "Feet")
{
input = "Feet";
}
else if (convert == "Yards")
{
input = "Yards";
}
else
{
throw new System.ArgumentException("Incorrect conversion");
//Trying to loop my main program if an ArguementException is thrown here
}
return input;
}
我尝试过的事情:
} while (convert != "Meters" || convert != "Feet" || convert != "Yards");
Console.WriteLine("Continue on");
Console.ReadLine();
我尝试告诉它在convert不是Meters,Fet或Yards的情况下保持循环,但是在引发争论之后,我无法继续执行该程序。
抛出此System.ArguementException后,我能够继续我的应用程序吗,如果可以,我可以在while循环中输入什么以允许此操作?
问题是,您调用Values.Input()
的位置在try / catch语句之外,并且当它引发异常时,不会在catch中处理该位置。因此它将被赶上调用堆栈。尝试将Values.Input(..)
放在try / catch语句中。