【问题标题】:How do a break a loop outside a try/catch from within it?如何从其中的 try/catch 外部中断循环?
【发布时间】:2015-12-12 20:35:45
【问题描述】:

我正在为学校做一个简单的课堂项目,以试验 C# 中的继承和其他要点,但我在某个部分遇到了问题。我相信我需要在无条件的 while 循环中尝试捕获,以确保用户以正确的形式输入数据,但我还需要能够从错误处理代码中跳出循环。我在给我带来问题的代码下方添加了注释。

class Program : students
{
    static void Main(string[] args)
    {
        students stu = new students();

        Console.Write("Number of students are you recording results for: ");
        int studNum = int.Parse(Console.ReadLine());
        stu.setNoOfStudents(studNum);
        Console.WriteLine();

        for (int a = 0; a < studNum; a++)
        {
            Console.Write("{0}. Forename: ", a + 1);
            stu.setForname(Console.ReadLine());
            Console.Write("{0}. Surname: ", a + 1);
            stu.setSurname(Console.ReadLine());
            while (0 == 0)
            {
                try
                {
                    Console.Write("{0}. Age: ", a + 1);
                    stu.setstudentAge(int.Parse(Console.ReadLine()));
                    Console.Write("{0}. Percentage: ", a + 1);
                    stu.setpercentageMark(int.Parse(Console.ReadLine()));
                    stu.fillArray();

                    break;
                    // This is the block that gives me problems; the
                    // while loop doesn't break.
                }

                catch (Exception)
                {
                    Console.WriteLine("This must be a number.");
                }
            }
        }
    }
}

我没有收到错误,因为它在 try/catch 中,但 while(0 == 0) 循环从未中断,因此 for 循环无法迭代命令。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 它确实中断了,但 for 循环将继续并再次执行 while 循环。
  • @KevinKal 它在我的屏幕上看起来不像,a 保持为 0。如果 for 循环迭代,增量不应该增加吗?

标签: c# while-loop try-catch break


【解决方案1】:

试试这个而不是休息

bool stop = false;
while (!stop)
{
    try
    {
        // Time to exit the loop
        stop = true;
     }
     catch { ... }
}

【讨论】:

    【解决方案2】:

    break应该带你离开while loop。如果您一步步通过,break 会发生什么?

    我建议使用Int.TryParse:

    bool input_ok = false;
    int input;
    while (! input_ok)
    {     
         Console.Write("{0}. Age: ", a + 1);
         input_ok = int.TryParse(Console.ReadLine(), out input);
         if (input_ok)
            {
                stu.setstudentAge(input)
            }
    }
    

    while 循环应该一直运行,直到你得到合适的东西。 for loop 中的所有内容。

    【讨论】:

    • 谢谢,它实际上是在捕捉方法 fillArray()
    • 所以它从来没有到达break,但捕获有点模糊。我希望你解决了。
    【解决方案3】:

    这个问题误导break 确实是正确的方法(不需要标志和假异常)并且它有效(在您的情况下打破了while 循环)。您的代码中唯一不断循环的分支是您的 catch 块,但我猜这是故意的(否则 while 循环毫无意义)。

    【讨论】:

    • 你能解释一下为什么catch块做一个无限循环吗?
    • 显然是因为里面没有break语句,所以控制流继续。无限 while 循环的唯一退出点是 breakreturn 和未捕获的 throws。
    【解决方案4】:

    你可以试试这个方法

    添加假异常

    public class FakeException: Exception { }
    

    示例代码:

    try
    {
    
          //break;
          throw new FakeException();
    }
    catch(Exception ex)
    {
       if(ex is FakeException) return;
       //handle your exception here
    }
    

    【讨论】:

    猜你喜欢
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多