Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, ignore exception and continue code?

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++

like image 493
user788171 Avatar asked Oct 04 '11 20:10

user788171


People also ask

Can C handle exceptions?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

Does C have try except?

The try-except statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.

Does code continue after catch C++?

Basically, the computer will try to execute code in the try block. If it doesn't work, the computer will execute the code in the catch block. After executing code from either the try or the catch block the computer will run the rest of the code as normal.

How do I ignore an exception in Java?

To ignore an exception in Java, you need to add the try... catch block to the code that can throw an exception, but you don't need to write anything inside the catch block.


2 Answers

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*/ }
}   
like image 71
driis Avatar answered Oct 27 '22 07:10

driis


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.

like image 26
David Rodríguez - dribeas Avatar answered Oct 27 '22 06:10

David Rodríguez - dribeas