Re: RFC: (in head) Throws()?

From: Alex Rousskov <rousskov_at_measurement-factory.com>
Date: Mon, 13 Oct 2008 22:39:54 -0600

On Tue, 2008-10-14 at 14:12 +1300, Amos Jeffries wrote:
> > On Mon, Oct 13, 2008 at 1:26 PM, Amos Jeffries <squid3_at_treenet.co.nz>
> > wrote:
> >> Kinkie wrote:
> >>>
> >>> Hi all.
> >>>
> >>> I've recently found myself battlign with a certain difficulty in
> >>> formatting custom messages for thrown exceptions; in order to use
> >>> TextExceptions properly I'd have to set a stringstream up, fill it in,
> >>> get a c_str out and then Throw, leaking memory in the meantime because
> >>> the message would be double-copied and there'd be no reachable code to
> >>> free one of the copies. The current Throw() implementation is very
> >>> skewed towards servicing Must().
> >>>
> >>> All of this should be easily solvable by defining a Throws() macro
> >>> (possibly with an accompanying Musts call) which would offer semantics
> >>> similar to debugs(), and get rid of this wart in one easy sweep.
> >>>
> >>> Any opinions?
> >>>
> >>
> >> Um I don't see what your problem is. Or what you are trying to do.
> >>
> >> To me it looks like is highly geared around non-dependable char* and
> >> Must()
> >> is warped to fit TextException.
> >>
> >> "TextException::TextException(msg)"
> >> can take a local char buffer[], "constant string", or object.c_buf().
> >> it does xstrdup() and xfree() it properly on its own.
> >
> > Maybe it's me not seeing the obvious.
> >
> > How would you render an exception saying
> > "out of bounds (trying to access index X of Y)"
> > where X and Y are off_t 's?
>
> {
> char temp[256];
> snprintf(temp,256,"out of bounds (trying to access index %"PRId64" of
> %"PRId64")", X, Y);
> Throw(temp,__FILE__,__LINE__);
> }

My recommendation is to replace the above with an equivalent of

        Must(!out_of_bounds_condition);

until the time when somebody actually bothers to treat the "out of
bounds" exception differently from other exceptions. At that time, they
should replace the above with something along these lines:

        if (out of bounds condition)
                Throw(OutOfBoundsException(X, Y));

Where OutOfBoundsException class is a child of std::exception or
TextException that does the right formatting when asked for a
human-readable string. For the "right formatting" code, they can use
Amos' suggestion or perhaps a string stream.

The actual OutOfBoundsException class may also need to store the String
or Buffer in question to be useful.

HTH,

Alex.
Received on Tue Oct 14 2008 - 04:40:24 MDT

This archive was generated by hypermail 2.2.0 : Tue Oct 14 2008 - 12:00:05 MDT