Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end a C++ program after error?

Tags:

People also ask

How do I stop a program in C?

So the C family has three ways to end the program: exit(), return, and final closing brace.

What does exit () do in C?

In the C Programming Language, the exit function calls all functions registered with atexit and terminates the program. File buffers are flushed, streams are closed, and temporary files are deleted.

What does exit 0 do in C?

exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.

How do you exit a whole program?

You can use the exit() function to terminate the process at any point during step1() , step2() ...


I am refactoring an old code, and one of the things I'd like to address is the way that errors are handled. I'm well aware of exceptions and how they work, but I'm not entirely sure they're the best solution for the situations I'm trying to handle.

In this code, if things don't validate, there's really no reason or advantage to unwind the stack. We're done. There's no point in trying to save the ship, because it's a non-interactive code that runs in parallel through the Sun Grid Engine. The user can't intervene. What's more, these validation failures don't really represent exceptional circumstances. They're expected.

So how do I best deal with this? One thing I'm not sure I want is an exit point in every class method that can fail. That seems unmaintainable. Am I wrong? Is it acceptable practice to just call exit() or abort() at the failure point in codes like this? Or should I throw an exception all the way back to some generic catch statement in main? What's the advantage?