4

I have this C++ program with the following general structure

1st while (condition A == true)
  //some code for 1st loop

  2nd while (condition B == true)
    //some code for 2nd loop
    try
      //some code for try
    catch
      //condition B == false (supposed to leave 2nd loop and go back to first loop)

I want it to get out of 2nd loop when there's an exception and go back to 1st loop until condition B is tue again. As described above it doesn't work as I expect. What seems to be happening is that code gets stuck in catch and never leaves it.

How can I arrange it to make it work as I need?

Note: condition A is never false.

Jonas
  • 121,568
  • 97
  • 310
  • 388
perr0
  • 145
  • 2
  • 2
  • 5

4 Answers4

7

add the break keyword to the catch

Also notice that you have b == false; That is checking that b is equal to false, not setting b = false.

ajon
  • 7,868
  • 11
  • 48
  • 86
  • Thank you! And indeed, I made a double mistake with `condition == B` inside catch (here and in the code itself). Thanks for pointing that out too. This show I need to read about break and continue again. I was trying continue but didn't even think about break (thought that it would stop program execution completely). – perr0 Aug 31 '12 at 22:22
  • break will break out of the current for loop. Glad to help. – ajon Sep 01 '12 at 03:10
2
bool flag1 = true, flag2 = true;
while (flag1)
{
  // some work so that flag2 == true
  while (flag2)
  {
    try
    {

    }
    catch (...) // any exception happens
    {
        break;
    }
  }
}
0
1st while (condition A == true) 
  //some code for 1st loop 

  2nd while (condition B == true) 
    //some code for 2nd loop 
    try 
      //some code for try 
    catch 
    {
      //condition B == false (supposed to leave 2nd loop and go back to first loop) 
      break ;
    }

Notice: Please do not use, even in examples, things like condition A == true. It is better to use while (condition A).

Grzegorz
  • 3,207
  • 3
  • 20
  • 43
  • 1
    I think I will disagree here. I think it is totally fine to include ==true. It makes it more explicit and can at times be more readable. It is just a matter of coding conventions and styles. Personally, I do not include them, but I don't mind seeing them from other people. – ajon Aug 31 '12 at 22:19
  • No problem. I am not following my style, though, but what the coding gurus were teaching my at the university. I am passing this knowledge only. – Grzegorz Aug 31 '12 at 22:22
0

You can call break within the catch block to escape the second loop:

void foo(void) {
    bool A(true);
    while (A) {
        bool B(doSomething());
        while (B) {
            try {
                B = doSomethingElseThatMayThrow();
            } catch (...) {
                break;
            }
        }
     }
}

Alternatively, you could place the second loop inside the try block:

void foo(void) {
    bool A(true);
    while (A) {
        bool B(doSomething());
        try { 
            while (B) {
                B = doSomethingElseThatMayThrow();
            }
        } catch (...) {}
    }
}