Exiting program from inside void function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavan869
    New Member
    • Nov 2016
    • 1

    Exiting program from inside void function

    Is there any way to exit program directly from inside void function. I want exit the whole program not only the function.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Exiting from a function is a bad choice. Maybe the function thinks it's time to give up but the calling function may not think the same.

    Errors have different meanings based on where you are in the program.

    You call the exit() or abort() library but be aware that your C++ objects may not be cleaned up correctly and in a multi-threaded application you can cause a crash on another thread.

    Your void function should throw an exception which can be caught by the calling function. That function can decide whether it's OK to continue or not. If not, that function can throw an exception.

    So you can see that a series of exceptions are thrown as the error works up the call stack. If appropriate main() can just return.

    Comment

    Working...