#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2

    How to check library functions for return errors


    Should I check the return value or errno or both?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    I think it depend on your requirements.
    if you just want to know that your function is succeed or failed then there is no need to check errno.
    but if you want to know exact reason of error then you can check errno.

    return value only tell you that function was successful or failed but dont give you exact reason of failure.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    RTFM. Read The Manual.

    In the documentation for each function, the return values are described. Part of that description is what is returned in case of an error. That is not the same for every single function, so you need to RTFM.

    Once you have detected that error, that is when you use errno to find out what kind of error you got. Part of the documentation for a function is a list of the errors that it could encounter and what could have caused that error. You could also use a function like perror to print out a description for that error.

    BTW, errno is set to the last error encountered. If your function call did not result in an error, then errno would still be set to the last error encountered before your successful function call. It is therefore necessary to first detect whether an error occurred before you consult errno.

    Library function documentation can be found in the man pages, thousands of copies of which are on-line. Google on man page and the function you are looking up.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    If your function call did not result in an error, then errno would still be set to the last error encountered before your successful function call. It is therefore necessary to first detect whether an error occurred before you consult errno.
    Yes, I've read it a thousand times... "Set errno to 0 before calling a function."

    thanks

IMN logo majestic logo threadwatch logo seochat tools logo