Topic: exception handling


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/01/27
Raw View
Mikael Steldal writes:

> In g++ 2.7.2.1, this code compiles and links OK (with
> -fhandle-exceptions). At run-time, it doesn't print "quit()" but gives
> SIGSEGV and dumps core. Is this code conforming or undefined behaviour
> (I guess that only undefined behaviour is allowed to cause SIGSEGV in a
> conforming compiler)? If this code is wrong, why?

The code is correct, the implementation of exception handling of g++
may be buggy for your platform.  Exception handling support in g++
2.7.x is still in development; it should be more stable in g++ 2.8.0.
You haven't specified the target platform nor compilation flags (you
haven't asked for optimization, have you?), but I've tried your code
snippet (that I have not included in this followup) on
sparc-sun-solaris2.5 and i586-unknown-linux (1.2.8) and both worked
fine.


> try
> {
>  f();
> }
> // no catch statements here

> equvivalent to

> f();

> ?

try { /*...*/ } must be followed by at least one catch clause.  g++ is
wrong in allowing an empty handler-seq.  This bug should be easy to
fix, so I'm sending a copy of this message to the g++ development list.


--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: d96-mst@nada.kth.se (Mikael Steldal)
Date: 1997/01/25
Raw View
In g++ 2.7.2.1, this code compiles and links OK (with
-fhandle-exceptions). At run-time, it doesn't print "quit()" but gives
SIGSEGV and dumps core. Is this code conforming or undefined behaviour
(I guess that only undefined behaviour is allowed to cause SIGSEGV in a
conforming compiler)? If this code is wrong, why?

I tried to enclose the call to f() in a try, but it still SIGSEGVs.
BTW, does that make any difference, i.e. is

try
{
 f();
}
// no catch statements here

equvivalent to

f();

?

When I added a catch(int) to the try-block, it worked as expected.

#include <iostream.h>
#include <stdlib.h>

void quit(void)
{
 cout << "quit()" << endl;
 exit(0);
}

void f(int i)
{
    throw i;
}

int main(void)
{
 set_terminate(quit);

    f(4711);

    return 0;
}
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]