By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

What is the proper way to handle errors from function calls?

P: n/a
atv
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?

If i don't return them to main, except for the structure, what
use is the main function except for calling functions?

I hope you understand what i mean. It seems a bit silly to call a function
created by me, from main, then if a error occurs in that function, to
return the value to main and print a message there.

should i handle errors and print error messages in the called functions or
leave it up to the main() function?

What's the best way?
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
atv wrote:
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?
Opinions vary. My own opinion is that, if a function can intelligently
handle an error itself, then it should do so. If not, it should tell its
caller what went wrong, and let the caller decide what to do with it.
Applied recursively, this means that an error that nobody knows how to
handle will soon percolate through to main().

If i don't return them to main, except for the structure, what
use is the main function except for calling functions?
Heh - well, you got to start somewhere, and main() is where you got to
start. Typically, main() contains the extremely-high-level program logic.
This might be as simple as:

#include <stdlib.h>
#include "myinterface.h"

int main(void)
{
return RunMyProgram() ? EXIT_FAILURE : EXIT_SUCCESS;
}

or it may contain a high-level loop.
I hope you understand what i mean. It seems a bit silly to call a function
created by me, from main, then if a error occurs in that function, to
return the value to main and print a message there.
It depends on the situation, really. When your programs get really big,
you'll value having the control centralised, rather than distributed all
over your code.
should i handle errors and print error messages in the called functions or
leave it up to the main() function?
In little learning-style programs, it doesn't really matter all that much.
As the code grows, so do the problems of a decentralised error management
policy.
What's the best way?


The way that works for you. Try it both ways. See which you think is best
for you, and be prepared to change if you land a job where they see things
differently.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2

P: n/a
On Sat, 29 Nov 2003 11:13:45 UTC, atv <at********@xs4all.nl> wrote:
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?
The answer is simple: Yes.

If i don't return them to main, except for the structure, what
use is the main function except for calling functions?

I hope you understand what i mean. It seems a bit silly to call a function
created by me, from main, then if a error occurs in that function, to
return the value to main and print a message there.

should i handle errors and print error messages in the called functions or
leave it up to the main() function?

What's the best way?


Make a clean design of you program. Start on an abstract level. Break
the design down until anything is defined well.

There is nothing that can tell you how to handle an error!

Anyway return either success or an error code to tell the caller that
the fuction was finished well or that it fails. In case it failed it
would be often (but not always) a good idea to come with a more
specific error code to tell the caller more about the error. A fuction
may handle an error completely - but only if there is in noways the
possibility to recover from that error.

Breaking the whole run is mostenly a bad idea because you will avoid
any chance for the caller to do some error recovery or do a well
defined cleanup until it stops the whole work.

If it is possible and/or allowed by a function to print something to
stdout depends on the design of the whole program. Printing something
into a logfile may be a solution - but even that depends on the design
of the program. Anyway think about how meaningfull an error message
should be and if the function is able to tell the meaning of an error
instead printing only a stupid message. Mostenly it is more helpful to
let an higher level function print out an error message (stdout or
stderr or logfile instead of the low level one that knows nothing
about why the fuction gots called.

Again, a clean design will help you to design the error messages and
to define the kocations where they get printed and/or displayed.

--
Tschau/Bye
Herbert

To buy eComStation 1.1 in germany visit http://www.pc-rosenau.de

Nov 13 '05 #3

P: n/a
> 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?
you cannot do much in case of calling c functions
If i don't return them to main, except for the structure, what
use is the main function except for calling functions?
well !!!
I hope you understand what i mean. It seems a bit silly to call a function
created by me, from main, then if a error occurs in that function, to
return the value to main and print a message there.

should i handle errors and print error messages in the called functions or
leave it up to the main() function?

two points in favor of handling errors in the main function.
1) code looks much simple if we handle errors in main function (in
general callee).
2) If error handling is done in function code itself we lose potential
flexibility in handling them.

having return codes is especially useful in writing libraries
functions for use by other programs.

What's the best way?

depends on the function and the way(s) in which it is used..

Regards
Nilesh
Nov 13 '05 #4

P: n/a
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"?
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.