How is unexpected() defined?
Jack Reeves
jackw_reeves@hotmail.com
Thu Nov 1 07:51:00 GMT 2001
The KCC code is incorrect (which is suprising), the GCC result is as
expected. There are several aspects here. First, 'unexpected' is a built-in
function, which may, or may not, be replace-able by a client. You shouldn't
try to replace it anyway. The correct way to change the behavior of
'unexpected' is to create an 'unexpected_handler' function and call
'set_unexpected' (this is similar to how a new handler is installed).
Second, if you dig further into the Standard, you will discover that while
an unexpected handler may throw an exception, it still must be one that
matches the exception specification of the function. This is reasonable
since externally the exception specification has stated that only the
exceptions specified will ever propagate out of the function. Since your
'fun' has 'throw()' for the specification, then no exceptions can propagate
from it and any attempt to do so - no matter how you go about it - should
call terminate().
The default unexpeced handler is suppose to throw the standard exception
'bad_exception' if that is allowed by the throw specification, otherwise it
calls terminate().
>From: Maurizio Loreti <loreti@pd.infn.it>
>To: libstdc++@gcc.gnu.org
>Subject: How is unexpected() defined?
>Date: Mon, 12 Nov 2001 08:49:07 +0100 (CET)
>
>Hello - the current standard states that, if a procedure declares the
>exceptions that may be thrown and throws an exception not declared, the
>procedure unexpected() is called; and that in turn unexpected() may
>throw, or call terminate().
>
>I was playing with the following toy code:
>
>#include <iostream>
>#include <stdexcept>
>
>void fun() throw() {
> std::cout << "In fun()\n";
> throw std::runtime_error("ahi ahi");
>}
>
>int main() {
> try{
> fun();
> } catch (std::exception &e) {
> std::cout << "Exception: \"" << e.what() << "\"\n";
> }
> return 0;
>}
>
>namespace std {
> void unexpected() {
> cout << "In unexpected()\n";
> throw;
> }
>};
>
>Compiled and linked with KCC does what is expected, i.e.:
>In fun()
>In unexpected()
>Exception: "ahi ahi"
>
>Compiled and linked with g++ 3.0.2 it does not:
>In fun()
>Aborted (core dumped)
>
>Looking at the core file with gdb:
>MLO@mlinux 14 $ gdb ./foo core
>GNU gdb 5.0
>...
>(gdb) where
>#0 0x400b8d41 in __kill () from /lib/libc.so.6
>#1 0x400b89b6 in raise (sig=6) at ../sysdeps/posix/raise.c:27
>#2 0x400ba0d8 in abort () at ../sysdeps/generic/abort.c:88
>#3 0x40045685 in _ZN10__cxxabiv111__terminateEPFvvE (
> handler=0x400ba010 <abort>)
> at ../../../../gcc-3.0.2/libstdc++-v3/libsupc++/eh_terminate.cc:47
>#4 0x400456c0 in _ZN10__cxxabiv112__unexpectedEPFvvE (
> handler=0x400456c0 <_ZN10__cxxabiv112__unexpectedEPFvvE>)
> at ../../../../gcc-3.0.2/libstdc++-v3/libsupc++/eh_terminate.cc:57
>#5 0x400456d5 in _ZN10__cxxabiv112__unexpectedEPFvvE (
> handler=0x400456a0 <_ZSt9terminatev>)
> at ../../../../gcc-3.0.2/libstdc++-v3/libsupc++/eh_terminate.cc:63
>#6 0x4004558a in __cxa_call_unexpected (exc_obj=0x804f208)
> at ../../../../gcc-3.0.2/libstdc++-v3/libsupc++/eh_personality.cc:417
>#7 0x804a552 in _Z3funv ()
>#8 0x804a56e in main ()
>#9 0x400b29cb in __libc_start_main (main=0x804a560 <main>, argc=1,
> argv=0xbffff9d4, init=0x804a044 <_init>, fini=0x804c970 <_fini>,
> rtld_fini=0x4000ae60 <_dl_fini>, stack_end=0xbffff9cc)
> at ../sysdeps/generic/libc-start.c:92
>(gdb)
>
>shows that something like unexpected is called
>("_ZN10__cxxabiv112__unexpectedEPFvvE"), with a signature different
>from my own unexpected(). Grepping the include files for 'unexpected'
>shown nothing.
>
>The questions are:
>1) what g++ exactly does in these situations?
>2) the g++ behavior is not standard, is it? If it were, *my*
> unexpected had to be called.
>3) what am I supposed to do (command line options, ...) in order to
> force g++ to a standard behavior?
>
> Thank you in advance, M.
>
>--
>Maurizio Loreti
>http://www.pd.infn.it/~loreti/mlo.html
>Univ. of Padova, Dept. of Physics - Padova, Italy
>loreti@pd.infn.it
>
>
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
More information about the Libstdc++
mailing list