12

Is there a way allow execution of a bad block of code after an exception of thrown?

Presently, my code has an while loop that runs continuously. The code inside this while loop sometimes throws a vector out of range error. I have been unable to track down the cause of this particular exception, but ultimately, it doesn't matter much because the code inside the while loop does the same thing over and over again and the next iteration does not depend on the previous iteration in any way.

This, after the code within the while loop crashes, I would like it to start again from the top of the while statement.

Is there a way to accomplish this in C++? try/catch doesn't seem to work in this situation.

Additional Info: I would love to just take the code within the while loop, make it into its own executable, and put the while loop into a bash script, but there's some data each iteration requires that remains static and it takes too much time to re-load that data each time so I am forced to do my infinite while loop within C++

7
  • 2
    catch the exception in the scope of the loop and continue from there. But, really you NEED to find the root cause of your problem. Commented Oct 4, 2011 at 20:39
  • Can you please post some code to look at?. try{}catch(){} should be standard if you want a robust app Commented Oct 4, 2011 at 20:40
  • Exceptions are exceptional and should be handled. Even if you can't track down the problem. Commented Oct 4, 2011 at 20:41
  • Sounds like you want On Error Resume Next for C++. I cannot recommend it. Commented Oct 4, 2011 at 20:41
  • Can you show us how you tried to use the try/catch ? Because I think wrapping the whole code inside the while loop in a try block (and don't forget to document in the code why you don't care about this exception !) should achieve the desired effect. Commented Oct 4, 2011 at 20:43

3 Answers 3

12

You just need to catch the exception inside the while loop:

while(true) 
{
    try 
    {
          // your code
    }
    catch (Exception e) { /* Please, at least do some logging or other error handling here*/ }
}   
Sign up to request clarification or add additional context in comments.

3 Comments

} catch (std::out_of_range& exc) { std::cerr << exc.what(); }
Missing the exception type in that catch. I'd suggest catch(...) since his motivation for catch is not good design.
Is this even valid syntax? I get error: expected ‘(’ before ‘{’ token when I try a try catch statement without any parenthesis after catch.
1

The first thing that you should do is debug the code, for that you can probably run the code inside a debugger and diagnose what the problem is. Pushing the problem under the rug will not make it go away, and the program will still be buggy.

If on the other hand, the issue is with something that is truly exceptional but feasible (consider opening a file, sending a packet over the network, anything that could potentially fail, but is not expected to --as compared to something that should never happen), the try/catch approach should work.

Comments

0

if you could possibly post a snippet of code, we all could help you more. but in general you should always have some sort of error handling whether it be a try{}catch{} or just checking a variable like:

while(true)
{

if(flag == "Error")
{
//error handle
}

else
{
//continue with code execution
}

}

hope you get this problem solved!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.