A program can exit with a variety of different status codes.
I'd like to bind an exit handler as a catch all way of handling final tasks based on this status code.
Is it possible to dispatch on the status code from within the exit handler?
As far as I can tell, No.
Therefore, I am unable to obtain the status value, as shown in this small example:
#include <iostream>
#include <cstdlib>
int Get_Return_Code(){
//can this be implemented?
return 0;
}
void Exit_Handler() {
// how do I get the return code
// from within the exit heandler?
auto return_code = Get_Return_Code(); //?
// I'd like to make decisions based on the return code
// while inside my exit handler
if (return_code == EXIT_SUCCESS){
std::cout << "perform exit successful tasks...\n";
}
else {
std::cout << "perform exit failure tasks...\n";
}
}
int main(int argc, char** argv)
{
//bind the exit handler routine
if (std::atexit(Exit_Handler)){
std::cerr << "Registration failed\n";
return EXIT_FAILURE;
}
//if an argument is passed, exit with success
//if no argument is passed, exit with failure
if (argc > 1){
std::cout << "exiting with success\n";
return EXIT_SUCCESS;
}
std::cout << "exiting with failure\n";
return EXIT_FAILURE;
}
Is there a reason that C++ does not yet include on_exit?
I'm concerned about cross-compatibility in the windows world.
in regards to the code base:
My goal for doing this, does not involve memory management. We have an existing code base. There are exit statements everywhere. When a program exits with error, I'd like to display what that error code means to the user. In my mind, this is the fastest solution without major refactoring.
If exit_code is EXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned. Stack is not unwound: destructors of variables with automatic storage duration are not called.
Reports the termination when some error or interruption occurs during the execution of the program. The usage of exit (0) is fully portable. The usage of exit (1) is not portable. EXIT_SUCCESS is defined by the standard to be zero. EXIT_FAILURE is not restricted by the standard to be one, but many systems do implement it as one.
There are two types of exit status in C/C++: 1 Exit Success: Exit Success is indicated by exit (0) statement which means successful termination of the program, i.e. 2 Exit Failure: Exit Failure is indicated by exit (1) which means the abnormal termination of the program, i.e. some error... More ...
Exit Failure: Exit Failure is indicated by exit (1) which means the abnormal termination of the program, i.e. some error or interrupt has occurred. We can use different integer other than 1 to indicate different types of errors. Reports the successful termination/completion of the program. Reports the abnormal termination of the program.
Because cleanup tasks are supposed to be performed by your destructors, and your code is supposed to gracefully return from any scope under all circumstances (be it via return
or throw
).
at_exit
is an anti-pattern in a RAII-friendly world.
If you want to perform some logic depending on what you're about to return from main
, simply perform it when you're about to return from main
. In main
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With