Correct usage of exit() in c++?

后端 未结 6 1201
再見小時候
再見小時候 2021-02-12 10:31

I have written a simple application that reads a data file, parses the text and then does some processing on that data. The data file is opened in my main() function. Is it good

相关标签:
6条回答
  • 2021-02-12 10:58

    Exit is acceptable, although I believe it is important to note the differences in memory of using exit vs. a return statement in that exit will not destroy variables in memory. If there is some error, then exit is justified. Otherwise, I would stick to the return statement.

    0 讨论(0)
  • 2021-02-12 10:59

    There are two aspects. One is the interest of deciding to stop the program at the place you want to use exit, the other is the use of exit. Mat's answer covers the first.

    For the second, exit is usually a bad choice in C++. The reason is that it does some cleanup (functions registered with atexit and that sometimes include destructors of some objects of static storage duration), but not all of them (destructors of objects on the stack) and in my experience you either want all of it or none.

    0 讨论(0)
  • 2021-02-12 11:05

    From main there's no difference in exit(1) or return 1. You use a return/exit value of 0 for success and non 0 for failure.

    If your subroutine is a library routine, which is used somewhere else, it should return control to main with some return code or an exception. Otherwise it's your choice, if you exit or return.

    In either case, it's good practice to document what the function does, be it exit, return code or exception.

    0 讨论(0)
  • 2021-02-12 11:08

    Calling exit from a function isn't "bad" in the sense that it has well-defined behavior - there's nothing fundamentally wrong in doing so.

    But, if you're writing a function that could end up in a library for instance, calling exit from there is bad practice in general: it is much better to signal an error to the calling code (via a specific return value or exception for instance) and let the calling code decide what to do. (There are cases when it's perfectly valid though. e.g. if you're writing a function called quit_if_file_not_found, well, your users are expecting a termination.)

    In your case, your parsing function probably shouldn't call exit: you might want, for example, at some point in the future, your main code to ask the user for a different file name if parsing the first one failed. If your parsing routine terminates the program, you have to modify both your main code and that function. If it had signaled an error condition, you'd only have to modify the logic in main.

    (And don't just exit without printing an error message or logging something like you're doing above, that will make for frustrated users who can't know how to fix whatever issue it is the code encountered.)

    0 讨论(0)
  • 2021-02-12 11:13

    It depends on where that exit(1) comes from. You should not call this exit(1) from a library, only from you own application.

    If you need to set an error code, you may set an errno (the STD C variable, yes).

    If you want to try a more C++ way, you can throw an exception, with a detailed error code.

    0 讨论(0)
  • 2021-02-12 11:16

    exit(0) indicates successful program termination & it is fully portable, While

    exit(1) (usually) indicates unsucessful termination. However, it's usage is non-portable.

    0 讨论(0)
提交回复
热议问题