Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A failure to handle exceptions

59 views
Skip to first unread message

James Moe

unread,
Mar 26, 2016, 8:47:19 PM3/26/16
to
gcc v3.3.5

When the exception is thrown "throw error_handler(pmmsend_rc)" the
program aborts (SIGABRT). The constructor completes; the abort occurs
after initialization, presumably in the catch() statement.
I can see no reason for this unreasonable behavior.
Am I missing anything? Or should I blame the compiler?


class error_handler {
private:
const char * txt;
pmmsend_return_code_t rc;

public:
error_handler (pmmsend_return_code_t err_rc = RC_NO_ACTION) { rc =
err_rc; }
pmmsend_return_code_t return_code () const { return rc; };

const char * return_text () {
switch (rc)
{
default: txt = "Unhandled Error"; beak;
case RC_NO_ERROR: txt = "No Error"; break;
case RC_NO_ACTION: txt = "Nothing happened"; break;
case RC_HELP: txt = "Help displayed"; break;
// more to come...
}
return txt;
}
};

int main (int argc, char *argv[])
{
pmmsend_return_code_t pmmsend_rc = RC_NO_ACTION;
prog_args_t prog_args;

try {
if (RC_NO_ERROR != pmmsend_rc) // Make it fail
throw error_handler(pmmsend_rc);

// build message
// send message
}
catch (const error_handler & ex) {
cerr << "Error: " << ex.return_code() << endl;
}
catch (...) {
cerr << "Splat! The unhandled error." << endl;
}

exit(pmmsend_rc);

}



--
James Moe
jmm-list at sohnen-moe dot com

Ian Collins

unread,
Mar 26, 2016, 9:05:15 PM3/26/16
to
On 03/27/16 13:46, James Moe wrote:
> gcc v3.3.5

Why? That very old.

Try the code with a modern compiler and see if the problem is still there.

When I try your code. I get

Error:0

--
Ian Collins

Alf P. Steinbach

unread,
Mar 27, 2016, 12:35:02 AM3/27/16
to
On 27.03.2016 01:46, James Moe wrote:
>
> class error_handler {
> private:
> const char * txt;
> pmmsend_return_code_t rc;
>
> public:
> error_handler (pmmsend_return_code_t err_rc = RC_NO_ACTION) { rc =
> err_rc; }
> pmmsend_return_code_t return_code () const { return rc; };
[snip]

> if (RC_NO_ERROR != pmmsend_rc) // Make it fail
> throw error_handler(pmmsend_rc);

At this point you're copying the uninitialized pointer in member `txt`.

Copying (or even just inspecting) an indeterminate value of greater than
byte size is formally Undefined Behavior.

However, I'm sure that that's not your current problem, which seems to
be using a very old compiler version.

With a newer compiler you get access to such goodies as
`std::system_error`, which passes an error code, with default
interpretation as a Posix error.

Oh well, that class is badly designed, but, it will probably do the job.


Cheers & hth.,

- Alf

PS: Calling `exit` at the end of `main` is sort of redundant. That's
what happens anyway on return from `main`. You should just return. ;-)

Öö Tiib

unread,
Mar 27, 2016, 11:32:18 AM3/27/16
to
On Sunday, 27 March 2016 02:47:19 UTC+2, James Moe wrote:
> gcc v3.3.5

On what platform?

>
> When the exception is thrown "throw error_handler(pmmsend_rc)" the
> program aborts (SIGABRT). The constructor completes; the abort occurs
> after initialization, presumably in the catch() statement.
> I can see no reason for this unreasonable behavior.
> Am I missing anything? Or should I blame the compiler?

Impossible (I will explain at end) to tell from your post but there is
always little chance of compiler defect.

The gcc 3.3 is over 10 years old version. If it is really an issue
with it and it was not fixed in gcc v3.3.6 then it will be likely
never fixed and so you have to work around of it.

Better take gcc 4.9.3 or 4.8.5 if you don't want to risk with the
bleeding edge 5. series. Likelihood of compiler defect to be fixed
(if found) is lot greater with newer compilers.

Do not erase #includes and declarations and other important things
if you post a test. Always try the test code if it manifests your
problem or not.

>
> class error_handler {
> private:
> const char * txt;
> pmmsend_return_code_t rc;

'pmmsend_return_code_t' undefined, I assume enum.

>
> public:
> error_handler (pmmsend_return_code_t err_rc = RC_NO_ACTION) { rc =
> err_rc; }

The 'txt' member uninitialized. U

> pmmsend_return_code_t return_code () const { return rc; };
>
> const char * return_text () {
> switch (rc)
> {
> default: txt = "Unhandled Error"; beak;

The 'beak' is unknown identifier.

> case RC_NO_ERROR: txt = "No Error"; break;
> case RC_NO_ACTION: txt = "Nothing happened"; break;
> case RC_HELP: txt = "Help displayed"; break;
> // more to come...
> }
> return txt;
> }
> };
>
> int main (int argc, char *argv[])
> {
> pmmsend_return_code_t pmmsend_rc = RC_NO_ACTION;
> prog_args_t prog_args;

Unknown type 'prog_args_t' and unused variable 'prog_args'.

>
> try {
> if (RC_NO_ERROR != pmmsend_rc) // Make it fail
> throw error_handler(pmmsend_rc);
>
> // build message
> // send message
> }
> catch (const error_handler & ex) {
> cerr << "Error: " << ex.return_code() << endl;
> }
> catch (...) {
> cerr << "Splat! The unhandled error." << endl;
> }
>
> exit(pmmsend_rc);

replace 'exit' with 'return'.

>
> }

Can't reproduce your error. Highly likely you edited its reason away.
Keywords like 'beak' indicate that you never attempted to compile and
run posted code. But it may also be that compiler contained a defect.
IOW we are not psychic, sorry.

woodb...@gmail.com

unread,
Mar 27, 2016, 2:29:49 PM3/27/16
to
On Saturday, March 26, 2016 at 7:47:19 PM UTC-5, James Moe wrote:
> gcc v3.3.5
>
> When the exception is thrown "throw error_handler(pmmsend_rc)" the
> program aborts (SIGABRT). The constructor completes; the abort occurs
> after initialization, presumably in the catch() statement.
> I can see no reason for this unreasonable behavior.
> Am I missing anything? Or should I blame the compiler?
>
>
> class error_handler {
> private:
> const char * txt;
> pmmsend_return_code_t rc;
>
> public:
> error_handler (pmmsend_return_code_t err_rc = RC_NO_ACTION) { rc =
> err_rc; }
> pmmsend_return_code_t return_code () const { return rc; };
>
> const char * return_text () {
> switch (rc)
> {
> default: txt = "Unhandled Error"; beak;
> case RC_NO_ERROR: txt = "No Error"; break;
> case RC_NO_ACTION: txt = "Nothing happened"; break;
> case RC_HELP: txt = "Help displayed"; break;
> // more to come...
> }
> return txt;
> }
> };

That class name is misleading in my opinion.

>
> int main (int argc, char *argv[])
> {
> pmmsend_return_code_t pmmsend_rc = RC_NO_ACTION;
> prog_args_t prog_args;
>
> try {
> if (RC_NO_ERROR != pmmsend_rc) // Make it fail
> throw error_handler(pmmsend_rc);
>
> // build message
> // send message

I'm interested in what you are doing with messages. I'm
working on some software to build and send messages.

I have an offer to help someone on their project if
they are willing to use my software in their project.
Specifically, I'll donate 16 hours a week for 6 months
to a project that uses my software. If someone provides
me with a successful reference for this, I'll give them
$3,000 cash and a $2,000 investment in my company. There
are more details about this here -
http://webEbenezer.net/about.html .

That page has been titled "Making programming fun again"
for years. I've recently been enjoying a song called
"Everything Comes Alive" by an Irish band called "We are
Messengers." As ambassadors of The King, messages are
near and dear to us.

Maybe someone has had to put a project in mothballs for a
while. This could be an opportunity to resurrect your project.

Brian
Ebenezer Enterprises
http://webEbenezer.net

James Moe

unread,
Mar 28, 2016, 5:26:50 PM3/28/16
to
On 03/26/2016 09:34 PM, Alf P. Steinbach wrote:
>> > if (RC_NO_ERROR != pmmsend_rc) // Make it fail
>> > throw error_handler(pmmsend_rc);
> At this point you're copying the uninitialized pointer in member `txt`.
>
> Copying (or even just inspecting) an indeterminate value of greater than
> byte size is formally Undefined Behavior.
>
> However, I'm sure that that's not your current problem, which seems to
> be using a very old compiler version.
>
So it would seem. I added initializing "txt = NULL" with no change in
execution.
>
> Oh well, that class is badly designed, but, it will probably do the job.
>
Thanks?
I haven't done any C++ programming in over a decade, and hadn't done
much before then.

James Moe

unread,
Mar 28, 2016, 8:52:29 PM3/28/16
to
On 03/26/2016 05:46 PM, James Moe wrote:
> gcc v3.3.5
>
> When the exception is thrown "throw error_handler(pmmsend_rc)" the
> program aborts (SIGABRT).
>
After compiling with gcc v4.7.3, the abort stopped occurring.
0 new messages