using exit(1) to return from a function
linux gcc 4.4.1 C99
I am just wondering is there any advantage using the following开发者_Go百科 techniques. I noticed with some code I was reading the exit number went up in value, as displayed in this code snippet.
/* This would happen in 1 function */
if(test condition 1)
{
/* something went wrong */
exit(1);
}
if(test condition 2)
{
/* something went wrong with another condition*/
exit(2);
}
or doing the following and just returning:
/* This would happen in 1 function */
if(test condition 1)
{
/* something went wrong */
return;
}
if(test condition 2)
{
/* something went wrong with another condition*/
return;
}
exit() exits your entire program, and reports back the argument you pass it. This allows any programs that are running your program to figure out why it exited incorrectly. (1 could mean failure to connect to a database, 2 could mean unexpected arguments, etc).
Return only returns out of the current function you're in, not the entire program.
return
will basically return from the function and adjust the stack pointers appropriately to execute the next instructions, where as exit
will cause the program itself to terminate.
using exit()
in a function indicates the fatal error and program cannot recover and continue and hence, it has to be terminated.
exit
will not return from a function. It will exit from the entire program
I don't think you want to exit the entire program, right?
So just returning from the function would be fine.
/* This would happen in 1 function */
if(test condition 1)
{
/* something went wrong */
return; /*return type must be void in this case */
}
if(test condition 2)
{
/* something went wrong with another condition*/
return; /*return type must be void in this case */
}
You can also explicitly specify the return type of the function and use the return value to judge whether everything went fine or not.
You're asking us whether you should return error codes from your functions?
Well that depends upon how informative you want to be to your users. If you want to act like software usually acts and pop up a modal dialog that says
Something bad happened!
Then there's no need for return codes.
If, however, you want to your software to be useful to your users and let them know what happened, then you better provide some kind of diagnostic information (error codes in the least). Then you can pop up a message that says:
I can't open "foo.bar".
Does this file exist? Do you have read access to it? Is it on a network share? Maybe I should try again?
Usually this is so that the program running your program can take some intelligent decisions. For example if there is a wrapper script around your program foo, then it could check the exit argument using variable $$ and change the execution path:
exec foo if $$ eq '0': echo "Success" elif $$ eq '2': exec error-recovery-script
Alternatively, you yourself can execute echo $$ to see what was the exit code from the program.
If the idea is to return the value based on the test condition, prefer using return instead of exit.
To return 1, instead of exit(1), use return 1.
Not posting the reasons, as there was a detailed discussion for the same in this link.
精彩评论