[LC++]functions return error nums: good practice?
Dr Mark H Phillips
mark at austrics.com.au
Thu Nov 7 07:59:01 UTC 2002
On Thu, 2002-11-07 at 08:34, Davide Bolcioni wrote:
> Dr Mark H Phillips wrote:
> > Hi,
> > Suppose I have a function which, for certain combinations
> > of input, will be unable to compute a sensible result. One
> > way of dealing with this is to have the function return an
> > int, which will be 0 if everything is okay, and a non-zero
> > if it isn't. The particular non-zero chosen will indicate
> > the type of error which was encounted.
> How do you plan to return the actual result then ?
I return the result through reference parameters. Returning
an int error doesn't cause any problems because previously
the return type was void.
> The key point in my mind is: upon seeing one of the special
> "failure" values, is it the caller supposedly able to act upon
> it in some meaningful manner, is it likely that it will have
> to pass the bucket up the call chain or you have no idea yet ?
> Only the first case would suggest a "failure" value.
Yes, the first case. It is only a "failure" in a weak sense. Ie,
the caller will expect it to fail from time to time. The caller
won't be too upset by this, it will simply handle the situation
as it sees fit.
Previously I handled these "failures" within the function itself.
This was okay when I only called it from one place. But now I
call it from two different places and I want to do different
thinks with "failures" for each place.
> > 2. Return an error object rather than an int. This object
> > might contain information like a standard error message
> > string associated with the error. Whereas with the int
> > error, you might have an array of error messages stored
> > somewhere else. I don't know which one is better.
> This is a burden for the normal case, when your function
> does return a value.
Several values actually - and via reference parameters. Yes
I suppose I could bundle them up into an object and return
a single object, but what's wrong with using reference
parameters as an efficient way to return multiple values?
(I use the convention that all non-const reference parameters
are first in the parameter list so you just have to look on
the left to see what is returned.)
Dr Mark H Phillips
Research Analyst (Mathematician)
AUSTRICS - smarter scheduling solutions - www.austrics.com
Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
Phone +61 8 8226 9850
Fax +61 8 8231 4821
Email mark at austrics.com.au
More information about the tuxCPProgramming