On Apr 30, 9:43 am, "Martin T." <0xCDCDC...@[EMAIL PROTECTED]
> wrote:
> Hi all,
>
> I'm currently trying to come up with a decent exception base class for
> some modules of ours and was thinking about adding exception-chaining
> (yes, like in Java :-)
> The following points where im****tant for me:
> * Must inherit from std::exception
> * Should allow for a std::exception to be the start of the exception
chain.
> * throw by value / catch by const reference
I have been using a similar solution with similar goals for quite some
time now and so far pretty happy about.
The problem with your implementation IMO is that you burden every
class derived from chained_exception with often unused members that
contain information about std::exception based 'cause'. What I did to
avoid this was to have a dedicated type, let's call it,
std_exception_wrapper that did the job of extracting type_info, what()
from std::exception and exposed the same chained_exception interface
with clone() etc. The chained_exception itself ends up holding a
single pointer to possible cause.
The usage would be
catch(std::exception & e)
{
throw chained_exception(...., std_exception_wrapper(e));
}
Another thing I've done differently is to have a generic 'printer'
facility that allows me to do
std::cout << exception_printer(ex, <some params that might make
sense>);
This is more flexible than having a 'print' member.
If you don't strive for universal ****tability and only intend to
sup****t major modern platforms you also might want to store backtrace
information in the chained_exception. Makes debugging remote
installation problems even easier. ;-)
--
Eugene
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|