1,105,578 Community Members

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

Member Avatar
shaffatshah
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!!!

Member Avatar
deceptikon
Eternally Awesome
4,699 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 690 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
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.

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
deceptikon
Eternally Awesome
4,699 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 690 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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

Member Avatar
shaffatshah
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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....

Member Avatar
deceptikon
Eternally Awesome
4,699 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 690 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: