1

I am working on a simple class project for school to experiment with inheritance and other essentials in c# but I am having trouble with a certain part. I believe I need a try catch within an unconditional while loop to be certain the user enters the data in the correct form but I also need to be able to break out of the loop from within the error handling code. I have put a comment beneath the code that is giving me problems.

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.");
                }
            }
        }
    }
}

I'm not getting an error because its within a try/catch but the while(0 == 0) loop is never broken so the for loop cannot iterate the command. Can someone give me a solution to this?

Adam Higgins
  • 615
  • 1
  • 8
  • 23

4 Answers4

4

Try this instead of a break

bool stop = false;
while (!stop)
{
    try
    {
        // Time to exit the loop
        stop = true;
     }
     catch { ... }
}
theB
  • 5,695
  • 1
  • 24
  • 36
2

the breakshould take you out of the while loop. If you step through, what happens at the break?

I suggest using 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)
        }
}

the while loop should keep running until you get something that will fit. All that inide the for loop.

PScr
  • 439
  • 2
  • 11
1

The question is misleading. break is indeed the right way (no flags and fake exceptions needed) and it works (breaks the while loop in your case). The only branch in your code that keeps looping is your catch block, but that I guess is intentional (otherwise the while loop makes no sense).

Ivan Stoev
  • 159,890
  • 9
  • 211
  • 258
  • could you explain why the catch block is doing an infinite loop? – Bgl86 Sep 16 '15 at 11:14
  • apparently because there is no `break` statement inside, so the control flow continues. the only exit point for an infinite `while` loop are `break`, `return` and uncatched `throw`s. – Ivan Stoev Sep 16 '15 at 11:16
-2

You can try this approache

Add fakeexception

public class FakeException: Exception { }

sample code:

try
{

      //break;
      throw new FakeException();
}
catch(Exception ex)
{
   if(ex is FakeException) return;
   //handle your exception here
}
VJPPaz
  • 602
  • 4
  • 14
  • 1
    exceptions for flow control is considered an antipattern and considered no better than using `goto`. http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control http://programmers.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why Use them for when something exceptional happens, not in the normal course of events. Also, throwing exceptions is expensive in terms of performance. – user1666620 Sep 16 '15 at 10:29