Re: why it is wrong to say: throw std::exception("error msg");
Andrew Marlow wrote:
I hope that some kind person will post chapter and verse for why it is
wrong to say:
throw std::exception("some error text");
18.6.1 does not define a constructor that takes a string:
namespace std {
class exception {
public:
exception() throw();
exception(const exception&) throw();
exception& operator=(const exception&) throw();
virtual ?exception() throw();
virtual const char* what() const throw();
};
}
but 17.4.4.4/2 says: "An implementation can declare additional non-virtual
member function signatures within a class: [...] by adding a member function
signature for a member function name."
It is not "wrong" to rely on implementation-defined behavior, it only limits
you to compilers that implement this specific behavior. The usual advice is
to use standard features as much as possible and I think this is easily a case
where there's no need to rely on implementation-defined behavior.
The additional problem here is that you don't exactly know how an implementation
is handling this constructor. The easiest way would be to copy it to a
std::string member, but then you'd risk the case of throwing std::bad_alloc
instead of the original exception. On Visual C++ 2008, std::exception seems to
allocate a buffer on the heap with std::malloc().
In any case, even if it hurts to see this, you probably have better things to
do.
--
Jonathan Mcdougall
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]