Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Documented error values

Reply
Thread Tools

Documented error values

 
 
Albert
Guest
Posts: n/a
 
      02-01-2009
Hi, Santos wrote in
http://groups.google.com/group/comp....ivers&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'?
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-01-2009
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CommandName values, documented? Brian Simmons ASP .Net 2 02-12-2008 03:00 AM
documented $PROGRAM_NAME use in error? Alan E Derhaag Perl Misc 2 05-19-2006 10:17 PM
[Power Post 2000] where is it documented? All Things Mopar Computer Support 1 06-23-2005 01:18 PM
[Possibly a FAQ - but one I can't find documented] netspam@shic.co.uk XML 4 05-27-2005 10:04 PM
Where are error messages documented? clintonG ASP .Net 1 02-10-2004 02:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57