Documented error values

Discussion in 'C Programming' started by Albert, Feb 1, 2009.

  1. Albert

    Albert Guest

    Hi, Santos wrote in
    http://groups.google.com/group/comp...13a981b1809585b?q=greedy gift givers&lnk=nl&:

    > What you do when a function has failed will obviously be highly
    > dependant on the exact situation, but for small test programs (like
    > yours) you might want to print a useful message printing what went
    > wrong and where (function and source line number) and probably, exit.
    > Recovery strategies (even if they are possible) may be too advanced at
    > this point.
    >
    > An example:
    >
    > if (fscanf(file, "%d", &i) != 1) {
    > fprintf(stderr, "%s (%d): fscanf() failed.\n",
    > __FILE__, __LINE__);
    > exit(EXIT_FAILURE);
    > }
    >
    > With C99 you can also use the predefined identifier __func__ to extract
    > the name of the current function (as a string).


    > For checking the internal consistency of the program you can use the
    > macro assert in assert.h. If the expression passed to assert evaluates
    > to zero (logically false), assert will print a message and abort the
    > program. You can define the macro NDEBUG *prior* to including assert.h
    > to turn off all assertions.
    >
    > Many library functions also set the object 'errno' to some integer value
    > upon failure. To use this mechanism you need to include the header
    > errno.h and set errno to zero before the function in question is
    > called. Immediately after you need to check errno for the presence of
    > documented error values (like ERANGE, EDOM, EILSEQ and other
    > implementation defined values), and if so, take appropriate action. You
    > can translate the error codes in errno to implementation specific
    > messages using the strerror or perror.


    What do ERANGE, EDOM, EILSEQ represent and how do I 'check errno for
    the presence of [these] document error values'?
    Albert, Feb 1, 2009
    #1
    1. Advertising

  2. Albert wrote:

    > What do ERANGE, EDOM, EILSEQ represent and how do I 'check errno for
    > the presence of [these] document error values'?


    The identifiers represent the three macros defined in <errno.h> for
    standard values for errno. Any elementary C text should provide
    explanation. In short:

    EDOM means there is a domain error; that is, the space over which the
    function is defined does not include the argument. [Example: sqrt(-1)]

    ERANGE means there is a range error; that is, even though the
    mathematical function is defined for the argument, the result is not
    representable in the return type. [example: exp(DBL_MAX)]

    EILSEQ means there is an encoding error when translating a multibyte
    character sequence.

    How you check for these is covered in any elementary C text. An example:

    #include <errno.h>
    #include <math.h>
    #include <stdio.h>

    int main(void)
    {
    double x = -1, y;
    errno = 0;
    y = sqrt(x);
    switch (errno)
    {
    case 0: printf("sqrt(%g) = %g\n", x, y);
    break;
    case EDOM:
    printf("sqrt() is not defined for argument %g\n", x);
    errno = 0;
    break;
    case ERANGE:
    printf("sqrt(%g) is mathematically defined, but\n"
    "its result is not representable in a double.\n",
    x);
    errno = 0;
    break;
    default:
    printf("sqrt(%g) returned the implementation-defined\n"
    "errno of %d. Check the implementation documentation\n"
    "for clarification.\n", x, errno);
    errno = 0;
    break;
    }
    return 0;
    }
    Martin Ambuhl, Feb 1, 2009
    #2
    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. clintonG

    Where are error messages documented?

    clintonG, Feb 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    445
    nfedin
    Feb 10, 2004
  2. Replies:
    4
    Views:
    368
    Peter Flynn
    May 27, 2005
  3. Vegard Bakke

    Will Python exceptions be documented?

    Vegard Bakke, Jul 7, 2003, in forum: Python
    Replies:
    1
    Views:
    298
    John J. Lee
    Jul 7, 2003
  4. Brian Simmons

    CommandName values, documented?

    Brian Simmons, Feb 11, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    352
    Walter Wang [MSFT]
    Feb 12, 2008
  5. Paul Lalli

    documented $PROGRAM_NAME use in error?

    Paul Lalli, May 19, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    138
Loading...

Share This Page