Boost logo

Boost :

From: William E. Kempf (wekempf_at_[hidden])
Date: 2002-11-20 14:15:27


Fernando Cacciola said:
> I'm not sure if a boost exception class is *needed*, but I see no
> problem in having one.
> Anyway, IIF such an exception class is defined, I *strongly* encourage
> (as I did in the past) that it provides:
>
> virtual void raise() const
> {
> #ifndef BOOST_NO_EXCEPTIONS
> throw *this ;
> #endif
> }
>
> and that boost libraries throw such exceptions *only* by calling
> .raise(): i.e. never with a throw expression directly in the user code.

template <typename T>
void raise()
{
#ifndef BOOST_NO_EXCEPTIONS
   throw T();
#endif
}

template <typename T, typename P1>
void raise(const P1& p1)
{
#ifndef BOOST_NO_EXCEPTIONS
   throw T(p1);
#endif
}

// other such overloads if needed

void foo()
{
   raise<std::logic_error>("my logic error");
}

Is this not better? It works even with the standard exception types, no
modification to the implementation. Not a 100% solution for what you
want, since there's still exceptions that can be raised outside of Boost
code, but maybe it's enough.

I dislike the standard exception what(), since (with most of the exception
types) this requires dynamic allocation of the string, which may change
the type of exception thrown, and the string isn't language neutral. I'd
prefer a what() that returned a key which never had to be allocated, and
possibly could be used to index into a catalog of language neutral strings
for output. Maybe type_info::name() would be enough for a human readable
string, but there's two issues with this: it doesn't carry any extra data
(for example, an invalid_argument exception could indicate which argument
was invalid) and it's not easily used to index into language neutral
strings. But if there's anything a boost_exception base type should do,
it's address the what() issues.

William E. Kempf


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