Re: C++ exception error messages
James K. Lowden wrote:
Volker Lukas wrote:
Nothing, except that the exception object could contain a buffer
declared "mutable", and could try to generate a elaborate message,
Why mutable? std::string::c_str() is const. std::exception::what()
is const.
class X : public std::exception {
[...]
virtual char const* what() throw() {
try {
s = "...";
return s.c_str();
} catch(...) {
That assignment mystifies me. You elided anything that could be
assigned to s. It seems likely to me that the constructor for X would
initialize s.
The idea was that the exception object could store a number of (simple)
members, like the errno in effect immediately after the failed operation
and an enumerated value encoding the kind of attempted operation. This
is just an example of course, and might not always be applicable.
The what()-function could then form a meaningful message from these
members. For this, it would likely have to modify a mutable buffer of
some type.
See also Bo Perssons reply about stack unwinding possibly freeing a bit
of memory which would then be available in the exception handler, when
what() is first called. I first learned about this thought, rather
obvious in hindsight, when reading the Boost error handling guidelines.
So if I may, I would like to cite these guidelines here:
http://www.boost.org/community/error_handling.html
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]