C++ throw clause checking in function declarations at compile time

Gabriel Dos Reis gdr@merlin.nerim.net
Sun Jul 21 09:54:00 GMT 2002


Akos Maroy <darkeye@tyrell.hu> writes:

[...]

| When calling A::b(), libstdc++ aborts the program since there is class 
| thrown from A::b() that is not declared in the throw clause of A::b():
| 
| Program received signal SIGABRT, Aborted.
| 0x420278b1 in kill () from /lib/i686/libc.so.6
| (gdb) bt
| #0  0x420278b1 in kill () from /lib/i686/libc.so.6
| #1  0x420276b8 in raise () from /lib/i686/libc.so.6
| #2  0x42028e59 in abort () from /lib/i686/libc.so.6
| #3  0x400b6ff7 in __cxxabiv1::__terminate(void (*)()) (
|      handler=0x42028cc0 <abort>)
|      at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:47
| #4  0x400b7044 in std::terminate() ()
|      at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:57
| #5  0x400b7067 in __cxxabiv1::__unexpected(void (*)()) (
|      handler=0x400b7020 <std::terminate()>)
|      at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:63
| #6  0x400b6f02 in __cxa_call_unexpected (exc_obj_in=0x80498c0)
|      at ../../../../libstdc++-v3/libsupc++/eh_personality.cc:456
| #7  0x080485b2 in A::b() (this=0xbffff987) at sample.cpp:12
| 
| 
| This is all fine.

And corresponds to the mandated behaviour.

| But couldn't gcc check for such issues at compile time 
| already? As at that time it is already known that A::a() may throw an 
| Exception class as an exception, but A::b() may not. It is also clear 
| that A::b() does not handle this exception, thus it will propagate it 
| upwards. All information is available at that time already. (Java 
| compilers do this very checking at compile time.)

Yes, but C++ is not Java.  C++ definition does not allow a compiler to
reject that code.  Have a look at 15.4/10.
This issue is a topic of frequent debates with no definitive answer.
Maybe you might want to consult the archive of C++ newsgroups.

| Maybe gcc could give a warning, or there could be a compilation flag 
| that would make gcc give a warning in such cases?

You're welcome to contribute a patch.

-- Gaby



More information about the Gcc mailing list