N
none
Hi,
I have an exception class like so:
class CMyException
{
public:
// Streams for creating the message
std:stringstream msg;
std:stringstream short_msg;
CMyException() {}
CMyException(const CMyException &rhs) { copy(rhs); }
CMyException(const std::string simple_msg)
{ msg << simple_msg; short_msg << simple_msg; }
~CMyException() {}
CMyException &operator=(const CMyException &rhs) { copy(rhs); }
operator const char *() { return msg.str().c_str(); }
protected:
// std:stringstream cannot be copied, so copy() must
// only copy the internal strings
void copy(const CMyException &rhs)
{
msg << (rhs.msg.str().empty() ?
rhs.short_msg.str() : rhs.msg.str());
short_msg << (rhs.short_msg.str().empty() ?
rhs.msg.str() : rhs.short_msg.str());
}
};
It's kind of a work in progress. The idea is that you can set either the
"msg" or the "short_msg" or both. You could use it like so:
if (parse_failed)
{
CMyException e;
// Write long message
e.msg << "Something bad happened on line " << line;
e.msg << " in file " << filename << ".";
// Write short message
e.short_msg << "Something bad happened.";
throw e;
}
Then, the "catcher" can decide which message to display:
try
{
parse(something);
}
catch (CMyException e)
{
// Display the long error message
ErrorMessage(e.msg.str().c_str());
}
For convenience, I added the (const char *) type-cast operator so that you
could also do this, assuming you want the longer message:
catch (CMyException e)
{
// Display the long error message
ErrorMessage(e);
}
But something very strange happens. My ErrorMessage() function displays
garbage when I use the type-cast operator. It works fine if I write
"ErrorMessage(e.msg.str().c_str())" but not if I write
"ErrorMessage(e)". I traced into the ErrorMessage() function in both
cases, and what I see is that the actual const char * pointer is off by
exactly 256 bytes in the latter case. I just don't see the difference
between the two!
I am using Visual Studio 2005, if that makes a difference.
I have an exception class like so:
class CMyException
{
public:
// Streams for creating the message
std:stringstream msg;
std:stringstream short_msg;
CMyException() {}
CMyException(const CMyException &rhs) { copy(rhs); }
CMyException(const std::string simple_msg)
{ msg << simple_msg; short_msg << simple_msg; }
~CMyException() {}
CMyException &operator=(const CMyException &rhs) { copy(rhs); }
operator const char *() { return msg.str().c_str(); }
protected:
// std:stringstream cannot be copied, so copy() must
// only copy the internal strings
void copy(const CMyException &rhs)
{
msg << (rhs.msg.str().empty() ?
rhs.short_msg.str() : rhs.msg.str());
short_msg << (rhs.short_msg.str().empty() ?
rhs.msg.str() : rhs.short_msg.str());
}
};
It's kind of a work in progress. The idea is that you can set either the
"msg" or the "short_msg" or both. You could use it like so:
if (parse_failed)
{
CMyException e;
// Write long message
e.msg << "Something bad happened on line " << line;
e.msg << " in file " << filename << ".";
// Write short message
e.short_msg << "Something bad happened.";
throw e;
}
Then, the "catcher" can decide which message to display:
try
{
parse(something);
}
catch (CMyException e)
{
// Display the long error message
ErrorMessage(e.msg.str().c_str());
}
For convenience, I added the (const char *) type-cast operator so that you
could also do this, assuming you want the longer message:
catch (CMyException e)
{
// Display the long error message
ErrorMessage(e);
}
But something very strange happens. My ErrorMessage() function displays
garbage when I use the type-cast operator. It works fine if I write
"ErrorMessage(e.msg.str().c_str())" but not if I write
"ErrorMessage(e)". I traced into the ErrorMessage() function in both
cases, and what I see is that the actual const char * pointer is off by
exactly 256 bytes in the latter case. I just don't see the difference
between the two!
I am using Visual Studio 2005, if that makes a difference.