Boost logo

Boost :

From: Mark Rodgers (mark.rodgers_at_[hidden])
Date: 2000-06-07 03:18:50


From: David Abrahams <abrahams_at_[hidden]>
> From: "Mark Rodgers" <mark.rodgers_at_[hidden]>
> >
> > int n = stream_cast<int>(argv[1], 0);
> > if (n < 5)
> > {
> > std::cerr << "x must be a number greater than 4.";
> > return EXIT_FAILURE;
> > }
> >
> > Do we care whether the user typed "fred" or "3"? Possibly not.
>
> We surely don't care about the overhead of throwing an exception
> in this case.

Not in this case. In this case we care about the extra code we
would have to write otherwise. Using a stream_cast that throws
would be a bit verbose:

    int n;
    try
    {
        n = stream_cast<int>(argv[1]);
    }
    catch (boost::whatever &)
    {
        n = 0;
    }
    if (n < 5)
    {
       std::cerr << "x must be a number greater than 4.";
       return EXIT_FAILURE;
    }

> From everything I've seen, throwing an exception would be at least a good
> default response, and probably a good response always. Can you give an
> example where an exception would actually be a problem?

Not a problem, just less convenient, as shown above. I'm not arguing that
stream_cast shouldn't throw exceptions, or even that throwing exceptions
shouldn't be the default. I'm just suggesting that providing a version
that returns a default value is also useful, and if you provide it you
should allow the user to specify the default.

If the only version available was one that threw exceptions, I would
probably have limited use for it.

Mark


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