How do I make a C++ console program exit?

前端 未结 13 2419
执笔经年
执笔经年 2020-12-03 16:43

Is there a line of code that will terminate the program?

Something like python\'s sys.exit()?

相关标签:
13条回答
  • 2020-12-03 17:24

    if you are in the main you can do:

    return 0;  
    

    or

    exit(exit_code);
    

    The exit code depends of the semantic of your code. 1 is error 0 e a normal exit.

    In some other function of your program:

    exit(exit_code)  
    

    will exit the program.

    0 讨论(0)
  • 2020-12-03 17:25

    Allowing the execution flow to leave main by returning a value or allowing execution to reach the end of the function is the way a program should terminate except under unrecoverable circumstances. Returning a value is optional in C++, but I typically prefer to return EXIT_SUCCESS found in cstdlib (a platform-specific value that indicates the program executed successfully).

    #include <cstdlib>
    
    int main(int argc, char *argv[]) {
      ...
      return EXIT_SUCCESS;
    }
    

    If, however, your program reaches an unrecoverable state, it should throw an exception. It's important to realise the implications of doing so, however. There are no widely-accepted best practices for deciding what should or should not be an exception, but there are some general rules you need to be aware of.

    For example, throwing an exception from a destructor is nearly always a terrible idea because the object being destroyed might have been destroyed because an exception had already been thrown. If a second exception is thrown, terminate is called and your program will halt without any further clean-up having been performed. You can use uncaught_exception to determine if it's safe, but it's generally better practice to never allow exceptions to leave a destructor.

    While it's generally always possible for functions you call but didn't write to throw exceptions (for example, new will throw std::bad_alloc if it can't allocate enough memory), it's often difficult for beginner programmers to keep track of or even know about all of the special rules surrounding exceptions in C++. For this reason, I recommend only using them in situations where there's no sensible way for your program to continue execution.

    #include <stdexcept>
    #include <cstdlib>
    #include <iostream>
    
    int foo(int i) {
      if (i != 5) {
        throw std::runtime_error("foo: i is not 5!");
      }
      return i * 2;
    }
    
    int main(int argc, char *argv[]) {
      try {
        foo(3);
      }
      catch (const std::exception &e) {
        std::cout << e.what() << std::endl;
        return EXIT_FAILURE;
      }
      return EXIT_SUCCESS;
    }
    

    exit is a hold-over from C and may result in objects with automatic storage to not be cleaned up properly. abort and terminate effectively causes the program to commit suicide and definitely won't clean up resources.

    Whatever you do, don't use exceptions, exit, or abort/terminate as a crutch to get around writing a properly structured program. Save them for exceptional situations.

    0 讨论(0)
  • 2020-12-03 17:30

    exit(0); // at the end of main function before closing curly braces

    0 讨论(0)
  • 2020-12-03 17:32

    throw back to main which should return EXIT_FAILURE,

    or std::terminate() if corrupted.

    (from Martin York's comment)

    0 讨论(0)
  • 2020-12-03 17:33

    While you can call exit() (and may need to do so if your application encounters some fatal error), the cleanest way to exit a program is to return from main():

    int main()
    {
        // do whatever your program does
    
    } // function returns and exits program
    

    When you call exit(), objects with automatic storage duration (local variables) are not destroyed before the program terminates, so you don't get proper cleanup. Those objects might need to clean up any resources they own, persist any pending state changes, terminate any running threads, or perform other actions in order for the program to terminate cleanly.

    0 讨论(0)
  • 2020-12-03 17:34
    #include <cstdlib>
    ...
    exit( exit_code );
    
    0 讨论(0)
提交回复
热议问题