On Aug 3, 2:41 am, JackC <jeche...@gmail.comwrote:
If I have a long boolean function
You have a problem. Functions shouldn't be long.
that has regular error checking
throughout, and simply returns false upon an error,
Not a very good choice, I would think. A boolean says true or
false; it answers a yes or no question. ("isEmpty()", for
example.) It doesn't say succeeded or failed, much less why
something failed.
whats the best approach to getting detailed information about
the error in the code segment that called the function?
Returning the information in the return value.
For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...
}
How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?
I doubt that you want a detailed error message at this point;
just the necessary information so that higher level code can
formulate the message. (At this level, you probably don't want
to be concerned with issues of internationalization, for
example.)
My theoretical approach, which might be wrong is as follows:
I would create a public variable in my class : string LAST_ERROR;
then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.
Sounds horrible. Just because Unix and Windows have adopted
more or less this approach (errno, GetLastError) doesn't mean
that it's a good idea.
Is this the best approach to error handling? I have heard
about using error codes, but i cant think how i this could be
implemented, or if there is a more standardized method.
The usual situation, when there is a need only to report an
error, and no additional need for information in the absense of
an error, is tu use a return code. Just define an enum with OK
andthe possible errors, and return it. Or wrap it in a class,
which will abort if the destructor is called without the error
having been checked.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34