1.11M Members

exiting a function in case of error while not terminating the whole program

 
0
 

hello everybody

void func1(){
// do something

//exit the function if some error occurs (like segmentation fault) but continue running the rest of the program
}

void func2(){

//do something

//exit the function if some error occurs (like segmentation fault) but continue running the rest of the program

}

int main(){
func1();
func1();
}

Ok for the above program, if any error (such as segmentation fault) occurs in func1, i want the function to end but continue running the rest of the program (i.e func2). I have used exit() and return 0, but not working. HELP!!!

 
0
 

Return from the function with a return statement and the program will continue to run from that point on. But if the program depends on results from the function that errored, you'll have problems later on.

 
0
 

Return from the function with a return statement and the program will continue to run from that point on

This is quite hard to do considering that "some error" can be a segfault. The OP goal is achieved via setjmp in the caller and longjmp from the signal handler.

 
0
 

Oh, I didn't see that a seg fault was one of the potential errors. My bad.

 
0
 

Return from the function with a return statement and the program will continue to run from that point on. But if the program depends on results from the function that errored, you'll have problems later on.

Thanks a lot for your reply... but truely speaking i am a newbie in C, is it return 0 or return 1? the function is independent, its a test program, it executes and ends there, nothing is return, function starts with a void....

 
0
 

function starts with a void....

Then you would use an empty return statement:

void foo()
{
    ...

    if (some error)
    {
        return;
    }

    ...
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: