使用exit(1)从函数返回

ant*_*009 4 c c++

linux gcc 4.4.1 C99

我只是想知道使用以下技术是否有任何优势.我注意到我正在阅读一些代码,我正在读取退出编号的值,这个代码片段中显示了这个值.

/* 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);
}
Run Code Online (Sandbox Code Playgroud)

或者执行以下操作并返回:

/* 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;
}
Run Code Online (Sandbox Code Playgroud)

Tyl*_*ith 23

exit()退出整个程序,并报告您传递的参数.这使得正在运行的程序程序,以找出它为什么退出不正确.(1可能意味着无法连接到数据库,2可能意味着意外的争论等).

返回仅返回您当前所在的函数,而不是整个程序.


aJ.*_*aJ. 6

return将基本上从函数返回并适当调整堆栈指针以执行下一条指令,其中as exit将导致程序本身终止.

exit()在函数中使用表示致命错误,程序无法恢复并继续,因此必须终止它.