2012-10-27 13 views
0

我想要做的就是使用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 

} 
+1

重新启动是什么意思?如果你处理了这个异常,程序将不会终止。 – Oded

回答

1

只要它是一个控制台应用程序,你可以在catch块调用Main方法。

private static int m_NumberOfRetries = 5; //Define how many times application can "restart" itself to avoid stackoverflow. 

static void Main(string[] args) 
{ 
    try 
    { 
     //Do something useful 
    } 
    catch 
    { 
     m_NumberOfRetries--; 
     if (m_NumberOfRetries != 0) 
     { 
      Main(args); 
     } 
    } 
} 

但是这样做不是一个好习惯。您可以通过检查应用程序中的用户输入来避免此情况。

+2

如果他真的不走运,这也会导致溢出/内存错误。 – 9ee1

+0

@agarhy是的,你是对的,这是可怕的方式来做到这一点。实际上可以通过定义'catch'块可以调用'Main'多少次来避免堆栈溢出。我会编辑答案。 – Leri

+0

如果您在递归调用之前不递减m_NumberOfRetries,我认为它不会按照您现在的方式执行 – Kevin

1

在对其进行操作之前,您应该考虑验证输入。考虑创建一个专门的方法来接受用户输入并进行验证。该方法可以在内部继续询问用户输入,直到验证成功。

0
// Starts a new instance of the program itself 
System.Diagnostics.Process.Start(Application.ExecutablePath); 

// Closes the current process 
Environment.Exit(0); 
+0

如果人们实际上对不正确的用户输入做了这些操作,我会感到非常惊讶。如果他们这样做,我很想问他们为什么:) – 9ee1

+0

@agarhy我太不懂重启只是因为用户未通过验证的应用程序的原因。但是,当你写的建议,我认为写下代码以重新启动应用程序:=) –

0

只考虑再次开始读取输入。 如果它是你所需要的,下面是适合的:

将getData()方法放在Distance类中。类Distance必须具有非参数构造函数。

Distance.java

private double latitude; 
private double longitude; 
private double elevation; 
private double latitude2; 
private double longitude2; 
private double elevation2; 

public Distance() {} 

public boolean getData(){ 
    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]); 
    int pointNumber2 = Convert.ToInt16(pointInput[1]); 
    try{ 
     latitude = (Convert.ToDouble(items[pointNumber * 3]));   // 
     longtitude = (Convert.ToDouble(items[(pointNumber * 3) + 1])); // 
     elevation = (Convert.ToDouble(items[(pointNumber * 3) + 2]));  // 

     latitude2 = (Convert.ToDouble(items[pointNumber2 * 3]));   // 
     longtitude2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 1])); // 
     elevation2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 2])); // I assume the exception goes from these 6 lines    

     return true; 
    } catch (IndexOutOfRangeException) { 
     return false; 
    } 
} 

Main.java:

Distance curDistance = new Distance(); 
while(!curDistance.getData()) 
    Console.WriteLine("You have selected a point number outside the range of the data entered, please select two new pointnumbers"); 
Console.WriteLine("The distance in km's to two decimal places is:"); 
Console.WriteLine(String.Format("{0:0.00}", curDistance.toDistance()) + "km"); 

while循环使得程序要求输入,只要它是不正确。

0
catch() 
{ 
    console.writeline("Some error"); 
    private void restart() 
    { 
    //write Press any key to restart the program 

    //clear the screen; 

    //call the main method; 
    } 
} 
2

当您开始学习编程时,您会发现可以使用while或do-while循环来解决这些情况。因此,我会给你这样的答案:

  bool restart = false; 
      do 
      { 
       restart = false; 
       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"); 
        restart = true; 
       } 
      } while (restart); 

当心,如果您在主叫主,你可以最终使StackOverflowException :-D

+0

+1 A同时,这就是我将如何去 –

0

有用户数据输入问题和程序执行有很大的不同异常,你应该有两个独立的机制来处理每个异常。

用户数据输入问题(例如用户在需要输入数字的地方输入“abc”)应由输入验证机制(由您编写)处理。这种类型的问题是用户可以纠正的,通常会向用户显示验证失败的原因,并有机会重新输入数据。

用户无法更改程序执行异常(例如,尝试连接到数据库的超时已发生),应该使用语言中内置的try/catch机制来处理。

由于很多原因,认为使用try/catch机制来提供程序流控制(这是您试图执行的操作)的编程习惯很差。

相关问题