![]() |
|
exit(EXIT_FAILURE) vs. abort vs. terminate |
Martin Kalbfuß
Member #9,131
October 2007
![]() |
Hi, When to use exit(EXIT_FAILURE), abort or terminate? I'm not eben sure where's the difference between those three. Edit: I realized that terminate calls abort. http://remote-lisp.spdns.de -- my server side lisp interpreter |
Evert
Member #794
November 2000
![]() |
Use exit(EXIT_FAILURE) if you want to exit normally and have an error code that you may be able to test from the shell. Use abort if you want to flag a critical error and generate a core dump. |
torhu
Member #2,727
September 2002
![]() |
exit() will call allegro_exit() for you, abort() will not. exit() is basically a shortcut you can use when an error occurs, and exiting all the way through main is too much trouble. abort() is for the more extreme cases. If you're using C++, abort() won't call any destructors, while exit() will. |
ImLeftFooted
Member #3,935
October 2003
![]() |
std::terminate is a C++ feature that is typically called when an exception is thrown without a corresponding catch block. Typically you would override this with something that outputs the trouble to your log. There is also std::unexpected_handler which calls std::terminate by default. std::unexpected_handler is called when you throw an exception that is not allowed by the throw specifiers. // foo may throw *only* int, char, or short. void foo() throw(int, char, short) { throw float(25); } void main() { // Here std::unexpected_handler will be called which then calls std::terminate which then calls abort. foo(); // Here std::terminate will be called which calls abort. throw 5; } |
gnolam
Member #2,030
March 2002
![]() |
torhu said: If you're using C++, abort() won't call any destructors, while exit() will. Also, abort() is not guaranteed to perform any form of cleanup, such as closing open file handles, flushing buffered but unwritten data or deleting temporary files. exit() is. -- |
Martin Kalbfuß
Member #9,131
October 2007
![]() |
Thanks for your answers. But there are still questions left. Isn't a program termination without cleanup a bad thing? Even if the program doesn't work as it should? Is the core dump the only benefit of using abort? I'm not sure if there's any reason to call abort or terminate directly. http://remote-lisp.spdns.de -- my server side lisp interpreter |
Evert
Member #794
November 2000
![]() |
Martin Kalbfuß said: Isn't a program termination without cleanup a bad thing? Perhaps. Under normal circumstances, yes. But you wouldn't call abort() under normal circumstances. Quote: Is the core dump the only benefit of using abort?
Isn't it enough? Quote: I'm not sure if there's any reason to call abort or terminate directly.
There is if you want a core dump. |
Martin Kalbfuß
Member #9,131
October 2007
![]() |
So if I have a case where something can go wrong, I throw an exception. I can think of two cases. Please tell me what do you think about it. 1. I don't catch the exception. It's a programming error. I get the core dump and I'm able to debug my program. This kind of error shouldn't ever get in touch with the enduser. example: an array index out of range. 2. I catch the exception to handle it. (retry with different settings, or output an error message). This kind of error depends on the surroundings. System settings, data files or user input. It's not a programming error. example: couldn't find a bitmap file. http://remote-lisp.spdns.de -- my server side lisp interpreter |
|