How to check if the function would generate an Exit in C++

Asked

Viewed 72 times

1

I have a .lib with some functions. I defined that when the calculation of some function generates values outside the range allowed it to exit().

Now my C++ code that uses this lib need to calculate some of these functions but if the function I calculate in the middle of the path generate a Exit it aborts and does not calculate the others. Is there any way to check whether it returns in a Exit or not, and if you return skip the function?

I’m not sure if there would be another flag better for values outside the allowed range than the Exit. I would try a return Null; but some of these functions generate vector<double>

So what would be the best way to do that?

  • The function exit returns EXIT_SUCCESS ( Zero ) if the execution has been successful or EXIT_FAILURE if the execution has failed, both returns are macros. You can treat the range of invalid values with a simple if / switch ( Switch with techniques to cover ranges of values in each case ). It is worth indicating the use of abort in place of exit for your program to continue running normally.

  • Trying to solve this problem reminded me that answer about the parade problem

1 answer

5


There is no way. Your problem is another.

The problem is design. If you want to know if the function has achieved its goal or not use a form of communication that indicates this, do not use the exit(). This function should be used for severe cases of problems or for the normal closure of the application at some point where the goal is reached and nothing else should be done.

There are several techniques to tell if the function worked or not, in your case could use an exception, or some error code returned. If the nullptr does not answer, but is usually adequate, has other forms cited in the question linked.

Including testing if the function will work and then using it has the potential to generate a running condition, is not the proper way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.