473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
4 2434
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christopher T King | last post by:
Is it feasable, and/or desirable to have Python optimize tail-recursive calls, similar to Scheme and gcc -O2? i.e. effectively compile this: def foo(n): return foo(n-1) into this: def...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
14
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
1
by: Juan Romero | last post by:
Hi guys, Does anyone know how I can get a handle to the desktop or the screen? (of for this matter, the device context handle) I want a program to write some stuff there, but I can't seem to...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.