Documented error values

A

Albert

Hi, Santos wrote in
http://groups.google.com/group/comp...013a981b1809585b?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'?
 
M

Martin Ambuhl

Albert said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top