Thread: exiting a function during an error, while running the rest of the program

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    exiting a function during an error, while running the rest of the program

    hello everybody,

    Code:
    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 just that particular 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!!!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    If segmentation fault happens your program crashes

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shaffatshah
    if any error (such as segmentation fault) occurs in func1
    A segmentation fault is symptomatic of a programming error that causes undefined behaviour. Your recourse should be to fix this error.

    On the other hand, if you're talking about an error due to say, invalid input, or at least a programming error from the caller that can be detected (e.g., an invalid argument) then certainly, returning early from a function makes sense. This could be as simple as just testing for the error condition and then returning.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can not continue your program when you get a segmentation fault. When you get a segmentation fault your operating system stops your program because it tried to access memory, or some other resource that it does not own. You need to fix the cause of the segmentation faults, not try to ignore them.

    Jim

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is really a rather specialised case. It is not a substitute for proper error checking and handling.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <signal.h>
    
    sigjmp_buf  env;
    
    void func1 ( void ) {
      printf("func1 dies\n");
      {
        int *p = NULL;
        *p = 0;
      }
      printf("Never reached\n");
    }
    
    void func2 ( void ) {
      printf("Now func2 carries on\n");
    }
    
    void catcher ( int sig ) {
      if ( sig == SIGSEGV ) {
        siglongjmp(env,1);
      }
    }
    
    int main ( ) {
      signal(SIGSEGV,catcher);
      if ( sigsetjmp(env,SIGSEGV) == 0 ) {
        func1();
      } else {
        func2();
      }
    }
    
    
    $ gcc -D_POSIX_C_SOURCE bar.c
    $ ./a.out 
    func1 dies
    Now func2 carries on
    I'll leave you to RTM on all the functions, so you know what all the traps and pitfalls are
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not running yet runtime error???
    By scwizzo in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2008, 10:35 AM
  2. How to prevent exiting from running program
    By scorpio_IITR in forum Linux Programming
    Replies: 5
    Last Post: 01-18-2004, 04:15 AM
  3. Error when running a simple program
    By swishiat.com in forum C Programming
    Replies: 11
    Last Post: 12-14-2003, 06:53 AM
  4. Error when running program
    By cheeves in forum C Programming
    Replies: 6
    Last Post: 12-05-2003, 08:35 PM
  5. exiting program and function
    By jumpy in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 02:36 AM

Tags for this Thread