Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is bad_alloc not a runtime_error

15 views
Skip to first unread message

restor

unread,
May 27, 2009, 10:32:20 PM5/27/09
to
Hi,
Is there a reason for std::bad_alloc not being a subclass of
std::runtime_error?

Regards,
&rzej

--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

daniel....@googlemail.com

unread,
May 28, 2009, 6:27:14 PM5/28/09
to
On May 28, 4:32 am, restor <akrze...@interia.pl> wrote:
> Hi,
> Is there a reason for std::bad_alloc not being a subclass of
> std::runtime_error?

std::bad_alloc is a special beast which must be throwable
even under conditions where no memory is there ;-)

Note that std::bad_alloc does not have any constructor
that allows provision of a what argument, so presumably
an object of that type just returns a pointer of a char
sequence with static storage duration and a fixed content.

The latter is not true for runtime_error, so this exception
class must be capable to store an arbitrary dynamic
char sequence and must somehow use the free store
to satisfy the post conditions of the constructors.

Of-course these are straw-man arguments. It is possible
to combine both aspects with a sufficient interest. E.g.
instead of using the static storage duration of the string,
a single bad_alloc instance could be created during the
/dynamic initialization/ phase. But even this would not
be sufficient, e.g. the runtime_error class would have
to distinguish between normal copies and shallow copies
or it would need to add reference counting semantics of
a copy.

In any case, it seems that it was decided that the
expected burden on the implementation would be higher
than the advantage of a cleaner exception hierarchy.

To be fair: Since there does not exist very much of
an enforced exception hierarchy in C++ compared
to other languages (e.g. like Java), the expected
overall advantage for the end user is much more
limited.

Greetings from Bremen,

Daniel Kr�gler

Nevin :-] Liber

unread,
May 28, 2009, 6:28:00 PM5/28/09
to
In article
<8c04437a-d13a-4643...@g19g2000vbi.googlegroups.com>,
restor <akrz...@interia.pl> wrote:

> Hi,
> Is there a reason for std::bad_alloc not being a subclass of
> std::runtime_error?

std::bad_alloc has a no throw constructor. The constructor for
std::runtime_error takes a std::string as a parameter, so it may throw
during construction.

--
Nevin ":-)" Liber <mailto:ne...@eviloverlord.com> 773 961-1620

0 new messages