[LC++]functions return error nums: good practice?
Dr Mark H Phillips
mark at austrics.com.au
Fri Nov 8 14:00:01 UTC 2002
I thought I should tell the list my preliminary conclusions
about handling errors.
Exceptions are probably not appropriate in my situation
1. I expect these "failures" to occur as part of normal operation.
2. The failures can be handled by the calling code, so there is
no need for dramatic unravelling of the stack.
My initial approach was to return an int error number to flag
whether an error had occured or not, and if so, what type
of error. This has a few disadvantages:
1. It doesn't allow the passing of additional information
about the error. Eg the amount by which it failed, or information
needed to remedy the problem.
2. Returning an int works for functions which otherwise would
be void, but not for functions which already return something
I've found what seems to be a good solution. I use a non-const
reference parameter called errs, which the function uses to
convey whether an error occured, and if so, what error and any
additional information about it.
I am using the FC++ List datatype for errs, namely:
If the list is NIL (ie empty) then no error occured. If not,
the first string in the list will indicate what kind of error
occured. Depending on the error type, there may or may not be
additional strings in the list providing further info.
The nice thing about using FC++ lists is that they are easy
to work with and they take care of memory management for you.
Well, I hope this is useful to someone. And of course, I welcome
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