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

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?
 
R

Richard Heathfield

atv said:
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.
 
T

The Real OS/2 Guy

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.
 
N

Nilesh

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
 
G

Gordon Burditt

Whatis the proper way to handle errors from function calls?

Don't assume that the proper way to handle errors is unique.
That's sort of like asking a user to press THE key on THE keyboard.
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 probably need to do BOTH in some of your functions, especially
for non-recoverable errors. Check the error in the function itself
to determine that it failed, so at least you don't try to charge
ahead using a file that didn't open or whatever. Return error
status to the caller if you can't recover within the function itself.

Your lowest-level functions should probably operate completely
WITHOUT any user interface (printing error messages to the user)
unless their function IS the user interface. Consider how annoying
it would be if there were *NO* way to open a file that didn't prompt
the user for another file name if it couldn't find the file,
especially in a GUI where text-only prompts aren't even seen. Shells
written in C would constantly spit out error messages about optional
files that are missing (like .cshrc or .profile).

On the other hand, if the purpose of the function is to prompt
the user and get an answer, it seems quite reasonable to have
that function check the answer and ask the user for a valid value
if a wrong one is entered.
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.

It's not at all silly when your function is supposed to do something
simple, and it has no idea WHERE to print a message based on what
the program is doing (to stderr for straight text apps, to syslog
for daemons, and popping up an error box for GUIs) and it's potentially
usable in many programs. It is also irritating if EVERY function
has to be concerned with what human language the error messages
should be in.
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?

Gordon L. Burditt
 
P

Peter Shaggy Haywood

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"?
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top