Re: Should accessors throw exception ?
On Sep 13, 3:45 am, Ian Collins <ian-n...@hotmail.com> wrote:
James Kanze wrote:
On Sep 13, 12:55 am, Ian Collins <ian-n...@hotmail.com> wrote:
mathieu wrote:
I'd like to know if there is a general answer to the
following question: when making a public interface that
access a container should the function throw an exception
when the element is not found ? Otherwise I need two
functions: a Find and a Get function which put the burden
on the application programmer to always call Find before
Get.
I tend to favour returning NULL if the return type is a
pointer and throwing an exception if the return type is a
reference or an object without an obvious bad value.
Isn't this inversing cause and effect. I tend to choose to
return a pointer if I need a sentinel value, and a reference
otherwise.
Not really, consider dynamic_cast. I think you have given the same
answer I did in a different way.
Partially, but you seemed to be saying that you chose the means
of reporting errors according to the return value, where as I
would choose the type of the return value according to whether I
needed to report errors or not. (Supposing there is a choice,
of course. For constructors and overloaded operators, you often
don't have any choice but to throw an exception in case of
error.)
NULL is a sentinel value.
Which is why I choose to return a pointer if I need a sentinal
value. (In cases where I don't actually have something to
point to, for example if the function returns a calculated
value, I will use Fallible.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34