exit() call inside a function which should return a reference

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.

Leave a Comment