Re: un-catched Exceptions in mixed C/C++ code
On 2007-12-18 13:22:29 -0500, Kira Yamato <kirakun@earthlink.net> said:
On 2007-12-18 13:10:43 -0500, wrungel <wrungel@web.de> said:
On 18 Dez., 19:05, Kira Yamato <kira...@earthlink.net> wrote:
How did you get the linker to be able to let the C translation unit
(y.c) see the C++ function in the C++ translation unit (z.cc)?
Sorry that I couldn't refer to your source code. My Mac cannot
recognize the gtar format.
Using extern "C" declarations for y_func() and z_func().
I uploaded same code as zip archive:
http://freenet-homepage.de/wrungel/exceptions.zip
I think this is the problem: you've declared z_func() with extern "C".
No, that's got nothing to do with it.
The following test program
#include <iostream>
using namespace std;
extern "C"
{
void callc()
{
throw "oh no!";
}
};
int main()
{
callc();
return 0;
}
causes the runtime error message:
terminate called after throwing an instance of 'char const*'
Abort trap
As it should. There's no catch clause. If you wrap the call to callc()
in a try/catch block, this will work just fine.
So, I suppose declaring a function extern "C" changes the way it uses
the stack; hence giving you problems when that function throws an
exception.
No, extern "C" functions can still throw and catch exceptions if
they're compiled as C++.
It seems that there is no way to throw an exception across a stack
frame involving a genuine C function.
That's a different matter. A C function, compiled with a C compiler,
doesn't know anything about exceptions.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)