且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

exit() 在函数内部调用,该函数应该返回一个引用

更新时间:2023-11-24 12:43:34

这种情况很糟糕,程序无法继续,所以我打印了一条消息以帮助发现错误并调用 exit(1)

This situation is so bad that the program cannot continue, so I print a message to help to spot the bug and call exit(1)

没有.如果此代码是库的一部分,则库不应决定应用程序是否应退出.

No. If this code is part of a library, the library should not be the one deciding if the application should exit or not.

如果一个文件打开需要关闭,或者其他一些资源需要清理,或者你的数据库类的用户想要记录错误并继续做其他事情怎么办?

What if a file is open and needs to be closed, or some other resource needs to be cleaned up, or if the user of your DB class wants to log the error and continue doing something else?

答案是任何你现在在做什么.抛出异常、返回错误代码等,但不要在库或类代码中关闭应用程序.

The answer is anything but what you're doing now. Throw an exception, return an error code, etc. but don't close the application down within library or class code.

信不信由你,有一个商业数据库库可以完成你正在做的事情(关闭应用程序).他们从图书馆的用户那里得到了很多关于为什么他们意外关闭应用程序的愤怒回应.你知道吗——给客户的答案是我们觉得错误严重到足以停止应用程序,因为我们的库无法继续正常工作".这不仅是糟糕的推理,而且近乎傲慢,客户让他们知道这一点.

Believe it or not, there was a commercial DB library that did exactly what you're doing (closing the app down). They got a lot of angry responses from users of their library as to why they're shutting the application down unexpectedly. And you know what -- the answer given to the customers was that "we felt the error was severe enough to stop the application, because our library can't continue to work properly". That is not only bad reasoning, it borders on arrogance, and the customers let them know that.