error handlling in C

Discussion in 'C Programming' started by broli, Mar 28, 2008.

  1. broli

    broli Guest

    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.zeus", "r");
    if(zeus_file == NULL)
    {
    perror("File open error");
    return -1;
    }

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

    }
    broli, Mar 28, 2008
    #1
    1. Advertising

  2. broli

    Guest

    On Mar 28, 5:02 pm, broli <> wrote:
    > 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.zeus", "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>
    , Mar 28, 2008
    #2
    1. Advertising

  3. broli

    Flash Gordon Guest

    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.zeus", "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("alternate-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
    Flash Gordon, Mar 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,657
  2. JavaQueries
    Replies:
    1
    Views:
    3,651
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,092
  4. broli

    error handlling in C

    broli, Mar 28, 2008, in forum: C++
    Replies:
    1
    Views:
    294
    Ron Natalie
    Mar 28, 2008
  5. pereges

    error handlling in recursive function

    pereges, May 30, 2008, in forum: C Programming
    Replies:
    8
    Views:
    344
Loading...

Share This Page