[Exceptional C++ Style] Fw: Item 11: Try and Catch Me

Adrian Fagg adrianf at evaluategroup.com
Wed Dec 1 07:38:34 EST 2004


> -----Original Message-----
> From: effective-cpp-bounces at accu.org
> [mailto:effective-cpp-bounces at accu.org]On Behalf Of Kevlin Henney
> Sent: 01 December 2004 11:39

> >It seems to me, then, that the open routine cannot tell if
> the file is
> >essential or secondary, and so can't know whether to throw
> an exception or
> >not.
> >If, as has been suggested, there was an exception and non
> exception method
> >then the caller could choose. Having the constructor throw
> an exception and
> >open return fail value would fit the guidelines of
> exceptions being good for
> >constructors and return values emitted by operations that can.
>
> It might also be considered an inconsistent and indecisive design,
> trying to please all the people all of the time. Such accommodating
> interfaces tend to bloat as well as annoy users with their vague
> helpfulness :-) Choice is not of itself a virtue.

To me, practicality would suggest that a file class ought to consistently
throw exceptions for any method that fails - except closing the file in its
destructor perhaps ;-)

If that is built on a lower level API that returns error codes, that's OK by
me, as I wouldn't be using the lower level API to write applications.

In general, I'd expect exceptions to be thrown by higher level classes and
allow for the possibility of low-level code to eschew them.

I'd also rather (reluctantly) clutter an interface with extra querying
functions and throw exceptions for the main ones than return a null pointer
or whatever to indicate failure.
e.g.
  if(thisLot.Has("Jim"))
    DoIt(thisLot["Jim"]);

But as often as not, I'd rather skip the test, knowing that my optimistic
expectation that thisLot includes Jim should be satisfied.

[Names of objects or values of strings in this example bear no relation to
people on this list, or this list itself.]

Regards,

Adrian

Work: mailto:adrianf at evaluategroup.com
Home: mailto:adrian.fagg at ntlworld.com

Pretty pictures: http://homepage.ntlworld.com/rbyadf/




More information about the Effective-cpp mailing list