Error Handling by Libraries

Error Handling by Libraries - Lecture 6a Error Handling by...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 6a Error Handling by Libraries (Optional)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Error Flags -- <cerrno> To return the error flag in a global variable Global variable: errno The value of errno is set by some library functions such as the square root or atoi, possible values are: EDOM (domain error) For sqrt(x), the flag errno is set to the symbolic constant EDOM if x is not a numeric value or a negative value ERANGE (range error) For atoi(s), the flag errno is set to the symbolic constant ERANGE if s does not representa valid intger or if it’s not in the range of values that can be represented by integer
Background image of page 2
Example (1) #include <cerrno> double x = …; errno = 0; //clear the flag. //Note: no need to declare errno double d=sqrt(x); if (errno == EDOM) { handle error case … }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example (2) #include <cerrno> #include <stdlib> double g(char *s) { errno = 0; int n = atoi(s); if (errno == ERANGE) n=0; return …; }
Background image of page 4
Assertions One response to an exceptional condition is to simple halt execution
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Error Handling by Libraries - Lecture 6a Error Handling by...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online