Groovy hepcat atv was jivin' on Sat, 29 Nov 2003 12:13:45 +0100 in
comp.lang.c.
What is the proper way to handle errors from function calls?'s a cool
scene! Dig it!
Whatis the proper way to handle errors from function calls?
For example, i normally have a main function, with calls to mine
or c functions. Should i check for errors in the functions called
themselves, or should i return a code to main and handle the error
there?
It depends on the nature of the function and the error. It also
depends very much on the nature of the programmer.
But in general I prefer to let higher level functions (main() et al)
to do the error handling. The reason is mainly to make lower level
functions more reuseable. Lower level functions should generally do
one thing only, and not have to worry about how to handle errors.
Error handling is a separate step. Also I may want to handle errors
differently in different programs. For example, in a small, simple
hack program I may want to display an error message and quit, whereas
in a more polished program I may want to recover from the error
somehow. I find it more useful and flexible, in general, to return an
error code and let some higher level function take care of it.
Richard made a good point, though, that a function that can
intellegently handle its own errors should do so. But I rarely see a
low level function that can intellegently handle its own errors.
Writing output is usually a no-no in low level functions, IMHO. And
they certainly shouldn't call exit() or abort() (assert() calls
notwithstanding). Recovering from an error can make low level
functions much more complex, which is bad in a function that is
supposed to do only one single task.
Of course, every rule has exceptions.
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?