If I'm writing a class that will be used as an exception, what kinds of
things do I need to watch out for? For example, is it necessary to make
sure that the members of the class don't throw?
Suppose my class has one or more std::string members. These could throw
bad_alloc on construction or copy. Is this dangerous? Here's an example:
#include <string>
class SimpleException
{
std::string text;
public:
SimpleException (const std::string &s) : text(s) {}
const std::string &GetText() { return text; }
};
int main()
{
try {
throw SimpleException ("BORKED!"); /* what if constructor throws? */
}
catch (SimpleExceptio n e) { /* what if copying throws? */
// ...
}
return 0;
}
Thanks.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
things do I need to watch out for? For example, is it necessary to make
sure that the members of the class don't throw?
Suppose my class has one or more std::string members. These could throw
bad_alloc on construction or copy. Is this dangerous? Here's an example:
#include <string>
class SimpleException
{
std::string text;
public:
SimpleException (const std::string &s) : text(s) {}
const std::string &GetText() { return text; }
};
int main()
{
try {
throw SimpleException ("BORKED!"); /* what if constructor throws? */
}
catch (SimpleExceptio n e) { /* what if copying throws? */
// ...
}
return 0;
}
Thanks.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Comment