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

From: Gordon Burditt (gordonb.x2b3l_at_sneaky.lerctr.org)
Date: 11/30/03


Date: 30 Nov 2003 06:10:38 GMT


>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



Relevant Pages

  • Re: Cant get standby or hibernate to work on laptop
    ... Any error messages? ... If the Standby button is dimmed, ensure you have installed the drivers. ... spam999free@xxxxxxxxxx (remove 999 for proper email address) ...
    (microsoft.public.windowsxp.help_and_support)
  • error handling date
    ... I am trying to get the proper error messages to display if the user left a ... date field blank or did not put the date in proper format. ... formatted to short date and it shows the date in the proper format. ... ElseIf BPrd> EPrd Then ...
    (microsoft.public.excel.programming)
  • Problems finding gd.pm in perl 5.8.6 using linux
    ... I have installed the proper GD library and GD.pm for Perl/CGI and ... I get the following error messages when I ... funny thing is, even though I get these compilation errors, the shapes ...
    (comp.lang.perl.modules)
  • Trouble with GD in Perl 5.8.6
    ... I have installed the proper GD library and GD.pm for Perl/CGI and still ... I get the following error messages when I ... funny thing is, even though I get these compilation errors, the shapes ...
    (comp.lang.perl.misc)
  • Re: [JavaScript Appliation] error message
    ... The two error messages "...syntax error" and ...unable ... in order to see the effect of the interpreting of the script commands on the rendering ... > any options which are currently Enable be made Prompt where possible. ... > settings when you are through testing by using the dialog's Default Level button. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)