Boost logo

Boost :

From: Wynand Winterbach (wynand_at_[hidden])
Date: 2003-10-30 08:24:52


On Thu, Oct 30, 2003 at 02:03:26PM +0200, Peter Dimov wrote:
> Beman Dawes wrote:
> > At 03:22 PM 10/27/2003, Jessie Hernandez wrote:
> > >...
> > >Well, I personally prefer a "large" socket exception hierarchy. This
> > makes
> > >it very easy to catch specific exceptions and provides better error
> > >messages. With the current STL file classes, you may get errors
> > such as >"ios_base::failbit set!" (since there is only one exception
> > class, >ios_base::failure). This doesn't tell me why the failure
> > happened (does
> > the
> > >user have permissions to the file, etc.?) I'd rather have classes
> > such as >socket_base::connection_refused, etc., which have their
> > what() methods >overloaded to print out more specific messages.
> >
> > FWIW, the LWG just looked at a similar situation for regular
> > expressions.
> > The solution they prefer is for a single exception class for the
> > library, but adding a member function which will return a code which
> > identifies the specific reason for the failure. This member function
> > is in addition to what().
>
> FWIW, following that LWG precedent in all cases is dead wrong. ;-)
>
> If code needs to determine the specific reason for the failure, the
> specifity needs to be encoded in the exception type.
>
> try
> {
> f();
> }
> catch(reason_I_care_about const & x)
> {
> // appropriate action
> }
>
> The alternative forced upon users is
>
> try
> {
> f();
> }
> catch(general_failure const & x)
> {
> // who is that general failure character?
> if(x.code() == reason_I_care_about)
> {
> // appropriate action
> }
> else
> {
> // throw; // not one of ours
> }
> }
>
> which breaks the "never catch if you don't know how to handle" rule.
>
> Alex T will have something to add I'm sure, it also breaks his beloved two
> phase model. ;-)
>

I agree that the exception reason must be codified into the type name.
It is a bit of work to code like this, but I for one find it a lot easier
to understand the code (provided that proper names are given to exceptions).

Oh well, just my R0.02 (R = South Africa's currency, the Rand).

--Wynand


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk