Topic: Why is bad_alloc not a runtime_error


Author: restor <akrzemi1@interia.pl>
Date: Wed, 27 May 2009 20:32:20 CST
Raw View
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-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: daniel.kruegler@googlemail.com
Date: Thu, 28 May 2009 16:27:14 CST
Raw View
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


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





Author: "Nevin :-] Liber" <nevin@eviloverlord.com>
Date: Thu, 28 May 2009 16:28:00 CST
Raw View
In article
<8c04437a-d13a-4643-8e6e-371a66743c95@g19g2000vbi.googlegroups.com>,
  restor <akrzemi1@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:nevin@eviloverlord.com>  773 961-1620

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