There is question about using exit
in C++. The answer discusses that it is not good idea mainly because of RAII, e.g., if exit
is called somewhere
One reason to avoid exit
in functions other than main()
is the possibility that your code might be taken out of context. Remember, exit is a type of non local control flow. Like uncatchable exceptions.
For example, you might write some storage management functions that exit on a critical disk error. Then someone decides to move them into a library. Exiting from a library is something that will cause the calling program to exit in an inconsitent state which it may not be prepared for.
Or you might run it on an embedded system. There is nowhere to exit to, the whole thing runs in a while(1)
loop in main()
. It might not even be defined in the standard library.