Mail Archives: djgpp/1996/09/04/07:59:17
I have taken another look at this, and I got it
working this time.
I can confirm that the following program
*does* catch the int and write "Caught int 10":
--- Start of test.cc ---
#include <stdlib.h>
#include <iostream.h>
int
main(int, char**)
{
try
{
throw 10;
}
catch (int i)
{
cerr << "Caught int " << i << "\n";
}
cerr << "Done\n";
exit(0);
} // main(int, char**)
--- End of test.cc ---
And this more ambitious version works as well.
Uncomment whichever of the throw statements you like.
--- Start of test2.cc ---
#include <stdlib.h>
#include <iostream.h>
void fn();
int
main(int, char**)
{
try
{
fn();
}
catch (double d)
{
cerr << "Caught double " << d << "\n";
}
catch (int i)
{
cerr << "Caught int " << i << "\n";
}
catch (char c)
{
cerr << "Caught char \'" << c << "\'\n";
}
catch (char* s)
{
cerr << "Caught string \"" << s << "\"\n";
}
cerr << "Done\n";
exit(0);
} // main(int, char**)
void
fn()
{
cerr << "Made it into fn()\n";
// throw 12;
throw 12.2;
// throw "this is a sentence";
// throw 'q';
}
--- End of test2.cc ---
My problem was with optimisation switches.
The following definitely works:
gcc -fhandle-exceptions -o test.exe test.cc -lstdcx
and so does:
gcc -O2 -fhandle-exceptions -o test.exe test.cc -lstdcx
However, the -O3 optimisation switch (which I was using
by default) breaks the catch mechanism so that
terminate() is the result of every throw. I suppose this
to be because the exception handling code assumes things
about the stack that are not true after -O3 has done its
work (inlining functions, in particular, I think).
Notice that I have had to specify only the
stdcx library on the gcc command line. It contains
sufficient (all?) iostream modules for the above
programs, and the terminate() function required as the
failsafe handler for unexpected C++ exceptions. (I don't
use gxx. It's just an alias for gcc that includes C++
libraries by default, but it actually does not include
stdcx - the only one you need! - in DJGPP v2.00.)
Notice also that I have not had to specify any
special include files. This mechanism is part of the
language; not part of the library.
--
Ian Miller, Dorset, Africa
DJGPP 2.00, Win95 DOS box (LFN=n)
- Raw text -