c++ 中 exit() 的正确用法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14109358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct usage of exit() in c++?
提问by mahela007
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 programming practice to use the exit() function if it is determined that the file was not opened properly? eg:
我编写了一个简单的应用程序,它读取数据文件,解析文本,然后对该数据进行一些处理。数据文件在我的 main() 函数中打开。如果确定文件未正确打开,则使用 exit() 函数是一种良好的编程习惯吗?例如:
if (!file.is_open() ){
exit(1);
}
Furthermore, my program has a separate function to parse the data in the file. This function is called by main(). If the function finds an error in the data, I want the program to stop, after printing an error message. In such a situation, is it acceptable to use the exit() function within my parsing function? I am asking this question because, to me, it doesn't seem to be very tidy to allow a function to exit a program on it's own without returning control to the main() function. (I apologize if this question seems pretty obvious.. I'm new to C++ and programming in general).
此外,我的程序有一个单独的函数来解析文件中的数据。该函数由 main() 调用。如果函数在数据中发现错误,我希望程序在打印错误消息后停止。在这种情况下,在我的解析函数中使用 exit() 函数是否可以接受?我问这个问题是因为,对我来说,允许函数自行退出程序而不将控制权返回给 main() 函数似乎不是很整洁。(如果这个问题看起来很明显,我深表歉意。我是 C++ 和编程的新手)。
回答by Mat
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.
exit
从函数调用并不是“坏”的,因为它具有明确定义的行为 - 这样做从根本上没有错。
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.)
但是,如果您正在编写一个可能最终出现在库中的函数,那么exit
从那里调用通常是不好的做法:向调用代码发出错误信号(例如通过特定的返回值或异常)要好得多) 并让调用代码决定要做什么。(虽然在某些情况下它是完全有效的。例如,如果您正在编写一个名为 的函数quit_if_file_not_found
,那么您的用户正在期待终止。)
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
.
在您的情况下,您的解析函数可能不应该调用exit
:例如,您可能希望在将来的某个时候,如果解析第一个失败,您的主代码会要求用户提供不同的文件名。如果您的解析例程终止了程序,您必须修改主代码和该函数。如果它已发出错误信号,则您只需修改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.)
(并且不要只是exit
不打印错误消息或记录您在上面所做的事情,这将使沮丧的用户不知道如何解决代码遇到的任何问题。)
回答by AProgrammer
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 answercovers the first.
有两个方面。一个是决定在你想使用的地方停止程序的兴趣exit
,另一个是使用exit。Mat 的回答涵盖了第一个。
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.
对于第二个,exit
在 C++ 中通常是一个糟糕的选择。原因是它做了一些清理(注册的函数atexit
,有时包括一些静态存储持续时间对象的析构函数),但不是全部(堆栈上的对象的析构函数),根据我的经验,你要么想要所有这些,要么没有任何。
回答by Manish Nagar
exit(0)
indicates successful program termination & it is fully portable, While
exit(0)
表示程序成功终止并且它是完全可移植的,而
exit(1)
(usually) indicates unsucessful termination. However, it's usage is non-portable.
exit(1)
(通常)表示不成功的终止。但是,它的用法是不可移植的。
回答by Olaf Dietsche
From main
there's no difference in exit(1)
or return 1
. You use a return/exit value of 0
for success and non0
for failure.
从或main
没有区别。您使用返回/退出值表示成功,非表示失败。exit(1)
return 1
0
0
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.
如果您的子例程是一个库例程,它在其他地方使用,它应该将控制权返回给 main,并带有一些返回代码或异常。否则这是你的选择,如果你exit
还是回来。
In either case, it's good practice to document what the function does, be it exit
, return
code or exception
.
在这两种情况下,这是很好的做法,以文件函数功能,无论是exit
,return
代码或exception
。
回答by elcuco
It depends on where that exit(1)
comes from. You should not call this exit(1)
from a library, only from you own application.
这取决于它exit(1)
来自哪里。您不应该exit(1)
从库中调用它,而只能从您自己的应用程序中调用它。
If you need to set an error code, you may set an errno
(the STD C variable, yes).
如果您需要设置错误代码,您可以设置一个errno
(STD C 变量,是)。
If you want to try a more C++ way, you can throw an exception, with a detailed error code.
如果想尝试更C++的方式,可以抛出异常,并附上详细的错误代码。
回答by jHova
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.
Exit 是可以接受的,但我认为重要的是要注意使用 exit 与 return 语句在内存中的差异,该 exit 不会破坏内存中的变量。如果有一些错误,则退出是合理的。否则,我会坚持 return 声明。