Topic: Exception hierarchy
Author: pderocco@ix.netcom.com ("Paul D. DeRocco")
Date: Thu, 1 Apr 2004 07:49:37 +0000 (UTC) Raw View
> "Alberto Barbati" <AlbertoBarbati@libero.it> wrote
>
> 1) runtime_error and logic_error are highly dependent on the library
> implementation as they both depend on std::string. Having language-level
> features such as dynamic_cast and typeid depend so much on the library
> implementation is not acceptable. For this reason the std::exception
> class is so light-weight and have no other library dependencies.
That makes sense.
> 2) throwing a runtime_error/logic_error requires the dynamic allocation
> of one or more strings and it might be simply impossibile to do that in
> those peculiar situations that requires "low-level" exceptions. This is
> especially true for bad_alloc: if there's no memory to accomplish an
> allocation request, there might be no memory for the string too.
The implementations I've used all have some pre-allocated space for the
temporary exception objects themselves, so the difficult part is allocating
the subsidiary object that contains the data. But that can be solved by
using a reference counting implementation like Gnu does, and initializing
the string from a pre-existing static one.
> 3) it is not self-evident whether they should derive from runtime_error
> or from logic_error
I would say bad_alloc is a runtime_error, and the others are logic_errors,
generally speaking.
Another missing feature of exceptions, in my view, is a virtual clone()
function. This would be useful because an exception object disappears as
soon as the catch block is exited, and it is often useful to save the
exception for later processing. About all you can save now is the what()
string, and Gnu doesn't even put enough info into the string to completely
identify the exception if the type information is gone.
--
Ciao, Paul D. DeRocco
Paul mailto:pderocco@ix.netcom.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: pderocco@ix.netcom.com ("Paul D. DeRocco")
Date: Mon, 29 Mar 2004 06:03:27 +0000 (UTC) Raw View
It seems odd to me that the standard gives us runtime_error and logic_error,
yet the predefined exceptions bad_alloc, bad_cast, bad_typeid, etc. aren't
derived from either of them. Is there some reason for this?
--
Ciao, Paul D. DeRocco
Paul mailto:pderocco@ix.netcom.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Mon, 29 Mar 2004 18:49:07 +0000 (UTC) Raw View
Paul D. DeRocco wrote:
> It seems odd to me that the standard gives us runtime_error and logic_error,
> yet the predefined exceptions bad_alloc, bad_cast, bad_typeid, etc. aren't
> derived from either of them. Is there some reason for this?
>
1) runtime_error and logic_error are highly dependent on the library
implementation as they both depend on std::string. Having language-level
features such as dynamic_cast and typeid depend so much on the library
implementation is not acceptable. For this reason the std::exception
class is so light-weight and have no other library dependencies.
2) throwing a runtime_error/logic_error requires the dynamic allocation
of one or more strings and it might be simply impossibile to do that in
those peculiar situations that requires "low-level" exceptions. This is
especially true for bad_alloc: if there's no memory to accomplish an
allocation request, there might be no memory for the string too.
3) it is not self-evident whether they should derive from runtime_error
or from logic_error
--
Alberto
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]