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

This is a discussion on exiting a function during an error, while running the rest of the program within the C Programming forums, part of the General Programming Boards category; hello everybody, Code: void func1(){ // do something //exit the function if some error occurs (like segmentation fault) but continue ...

  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
    594
    If segmentation fault happens your program crashes

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,740
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03:15 AM
  3. Error when running a simple program
    By swishiat.com in forum C Programming
    Replies: 11
    Last Post: 12-14-2003, 05:53 AM
  4. Error when running program
    By cheeves in forum C Programming
    Replies: 6
    Last Post: 12-05-2003, 07:35 PM
  5. exiting program and function
    By jumpy in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 01:36 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21