473,231 Members | 1,831 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,231 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 2428
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.