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
- Next message: James Hu: "Re: passing parameter list of one function to another"
- Previous message: Paul Hsieh: "Re: Tokenizer interface"
- In reply to: atv: "What is the proper way to handle errors from function calls?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: James Hu: "Re: passing parameter list of one function to another"
- Previous message: Paul Hsieh: "Re: Tokenizer interface"
- In reply to: atv: "What is the proper way to handle errors from function calls?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|