我想要做的就是使用try-catch异常来重新启动程序,并让用户重新读取数据值。我将如何做到这一点?我尝试使用goto将其恢复到第一行,但这似乎不起作用。 (并且普遍的共识是goto's是邪恶的)。任何帮助,可以给予不胜感激。在catch括号中使用try-catch异常如果发生异常,我该如何重新启动程序?
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
string point = Console.ReadLine();
string[] pointInput = point.Split(' ');
int pointNumber = Convert.ToInt16(pointInput[0]); //Stores the actual input number's into two integers
int pointNumber2 = Convert.ToInt16(pointInput[1]);
try //Try-Catch statement to make sure that the User enters relevant PointNumbers
{
double latitude = (Convert.ToDouble(items[pointNumber * 3])); //
double longtitude = (Convert.ToDouble(items[(pointNumber * 3) + 1])); //
double elevation = (Convert.ToDouble(items[(pointNumber * 3) + 2])); //
double latitude2 = (Convert.ToDouble(items[pointNumber2 * 3])); //
double longtitude2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 1])); //
double elevation2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 2])); // Uses the relationship between the pointnumber and the array to select the required items from the array.
//Calculate the distance between two point using the Distance class
Console.WriteLine("The distance in km's to two decimal places is:");
Distance curDistance = new Distance(latitude, longtitude, elevation, latitude2, longtitude2, elevation2);
Console.WriteLine(String.Format("{0:0.00}", curDistance.toDistance()) + "km");
}
catch(IndexOutOfRangeException)
{
Console.WriteLine("You have selected a point number outside the range of the data entered, please select two new pointnumbers");
// here is where I would have the program restart
}
重新启动是什么意思?如果你处理了这个异常,程序将不会终止。 – Oded