error handlling in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • broli

    error handlling in C

    Is it generally a good idea to use perror() function to handle all the
    error situations ?

    For eg in one of my modules I have used it extensively.

    int reader()
    {
    FILE *zeus_file;
    ............... .....
    ............... ......

    zeus_file = fopen("sphere.z eus", "r");
    if(zeus_file == NULL)
    {
    perror("File open error");
    return -1;
    }

    ............... ............... ............... .

    }
  • vippstar@gmail.com

    #2
    Re: error handlling in C

    On Mar 28, 5:02 pm, broli <Brol...@gmail. comwrote:
    Is it generally a good idea to use perror() function to handle all the
    error situations ?
    >
    For eg in one of my modules I have used it extensively.
    >
    int reader()
    {
    FILE *zeus_file;
    ............... .....
    ............... ......
    >
    zeus_file = fopen("sphere.z eus", "r");
    if(zeus_file == NULL)
    {
    perror("File open error");
    return -1;
    }
    >
    ............... ............... ............... .
    >
    }
    If you afterwards decide to use another stream than strerr, then yes
    it's bad.
    Use fprintf(stream, %s%s\n", "File open error", strerror(errno) )
    strerror() is declared in <string.h>

    Comment

    • Flash Gordon

      #3
      Re: error handlling in C

      broli wrote, On 28/03/08 15:02:
      Is it generally a good idea to use perror() function to handle all the
      error situations ?
      As ever, it depends.
      For eg in one of my modules I have used it extensively.
      >
      int reader()
      {
      FILE *zeus_file;
      ............... .....
      ............... ......
      >
      zeus_file = fopen("sphere.z eus", "r");
      if(zeus_file == NULL)
      {
      perror("File open error");
      return -1;
      }
      >
      ............... ............... ............... .
      >
      }
      Now consider changing it to take the file name as a parameter and then
      doing something like:
      if (reader("main-config-file"));
      else if (reader("altern ate-config-file");
      else ...

      Sometimes you can handle the error without bothering the users, other
      times you want to log it to a file, sometimes send it to stderr. It all
      depends.
      --
      Flash Gordon

      Comment

      Working...