Do I need to do this:

Do I have to enclose subsequent calls to a function withint new try blocks each time?

Suppose when catching an error, if I clean up and retry it again, Will 1 try block suffice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int function(); //that throws an error

int main()
{
   try{
         function();
      }
    catch (...)
      {
          //cleanup
          //retry here:
          function();
      }
}


or do I have to do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void function(){return;}
int main()
{
     try
     {
         function();
     }
    catch (...)
     {
          try
             {
                 function();
             }
          catch (...)
             {
                  try{function();}catch(...){try{function();}catch(...){try{function();}catch(...){/*etc etc etc.*/}
                  }
                  }
             }
     }



}
closed account (zb0S216C)
A try block can contain any number of statements, just like any other block.

Wazzak
Looks like it's begging for a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  bool success = false;
  while (!success)
  {
   try
    {
      function();
      success = true;
    }
    catch (...)
   {
      //cleanup
    }
  }
}


A bigger question is why should it work the fifty-second time if it failed the first fifty-one times? Still, your code - your game :)
Last edited on
Thats not what I meant.

If I recall a function that is expected to throw an error, from a catch block of an try statement, does that catch block being 'attached' to the try block gurantee that when the function throws an error a second time it will be caught by the first and only try block?
or would this be better?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void function(){throw 1; return;}
int main()
{
    re:try
        {
            function();
        }
       catch (...)
        {
            //function();
            goto re;
        }
}
or would this be better?


That appears to be a standard loop done using an ugly goto. Why not just use a standard loop?
@Moschops: I agree thats probably better.
However, my code uses 're:try' which is just plain neat looking.

Anyways, what my code is doing is throwing an error on an expected event. Such as the end user getting bored with going through every iteration of a permutation. (actually subset iterator) which is taking a really long time. I have this recursive code that works really great, except if the list is big. So when the user presses a specified button, it will skip fro wherever it is the the for loop of which ever call on that stack that is at, will skip from the i'th iteration of the highest up call, to the i+1, which is nice, because i don't really need EVERY COMBINATION, but I do need to ensure that it goes through a sufficient # of them to generate a hard enough permutation. but as it turns out, by keeping it on the ith iteration of the highest level loop for too long, i'm not getting much variety, as every subset its going through so far has the same first elements, and subsequently is not being fast enough. This way I can wait for a little while, then skip to the next level, then wait for a while, then skip etc.

I'm working on a sudoku.



Topic archived. No new replies allowed.