简体   繁体   English

在c ++中正确使用exit()?

[英]Correct usage of exit() in c++?

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. 数据文件在main()函数中打开。 Is it good programming practice to use the exit() function if it is determined that the file was not opened properly? 如果确定文件未正确打开,使用exit()函数是否是一种很好的编程习惯? eg: 例如:

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(). 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? 在这种情况下,在我的解析函数中使用exit()函数是否可以接受? 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. 我问这个问题是因为,对我来说,允许一个函数自己退出程序而不将控制返回到main()函数似乎并不是很整洁。 (I apologize if this question seems pretty obvious.. I'm new to C++ and programming in general). (如果这个问题看起来非常明显,我道歉。我是C ++和编程的新手)。

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. 但是,如果您正在编写一个可能最终存在于库中的函数,那么从一般情况下调用exit是一种不好的做法:向调用代码发出错误信号要好得多(通过特定的返回值或异常,实例)并让调用代码决定做什么。 (There are cases when it's perfectly valid though. eg if you're writing a function called quit_if_file_not_found , well, your users are expecting a termination.) (有些情况下它完全有效。例如,如果您正在编写一个名为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. 在您的情况下,您的解析函数可能不应该调用exit :例如,您可能希望在将来的某个时候,如果解析第一个文件名失败,您的主代码会要求用户输入不同的文件名。 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 . 如果它已发出错误信号,则您只需修改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或者记录类似于您正在执行的操作,这将使那些无法知道如何解决代码遇到的问题的沮丧用户。)

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. 一个是决定在你想要使用exit的地方停止程序的兴趣,另一个是退出的使用。 Mat's answer covers the first. Mat的答案涵盖了第一个。

For the second, exit is usually a bad choice in C++. 对于第二种, exit通常是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. 原因是它做了一些清理(用atexit注册的函数,有时包括静态存储持续时间的某些对象的析构函数),但不是所有这些(堆栈中对象的析构函数),根据我的经验,你要么全部都需要它或者没有。

exit(0) indicates successful program termination & it is fully portable, While exit(0)表示程序终止成功并且完全可移植,而

exit(1) (usually) indicates unsucessful termination. exit(1) (通常)表示不成功终止。 However, it's usage is non-portable. 但是,它的使用是不可移植的。

From main there's no difference in exit(1) or return 1 . mainexit(1)return 1没有区别。 You use a return/exit value of 0 for success and non 0 for failure. 使用返回/退出值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. 如果您的子例程是一个在其他地方使用的库例程,它应该使用一些返回代码或异常将控制权返回给main。 Otherwise it's your choice, if you exit or return. 否则,如果您exit或退货,这是您的选择。

In either case, it's good practice to document what the function does, be it exit , return code or exception . 在任何一种情况下,最好记录函数的功能, exitreturn代码或exception

It depends on where that exit(1) comes from. 这取决于exit(1)来源。 You should not call this exit(1) from a library, only from you own application. 您不应该从库中调用此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 ++方式,可以使用详细的错误代码抛出异常。

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. 退出是可以接受的,虽然我认为重要的是要注意在退出中使用exit与return语句的内存差异不会破坏内存中的变量。 If there is some error, then exit is justified. 如果有错误,则退出是合理的。 Otherwise, I would stick to the return statement. 否则,我会坚持退货声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM