This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Problem with exception handling
- To: egcs-bugs at cygnus dot com
- Subject: Problem with exception handling
- From: Andrew McClure <andrew at amac dot com dot au>
- Date: Sat, 27 Feb 1999 22:12:55 +1300
Hi
I am currently getting up to speed with C++,
so please be patient if I've missed something obvious.
The code below is taken from the C++ FAQ Book
( example FAQ-271.C )
I copied and tried compiling this code as I was having
trouble with my own attempts to familiarise myself
with exception handling.
The code was compiled with g++ 2.8.1 and
binutils 2.9.1.0.15 ( upgraded as a fix attempt ) -
> g++ -g -O err.cpp -o x
When run I get the following output:
> ./x
> trying: throwing a Fred; Aborted (core dumped)
Any help much appreciateed
cheers
Andrew
Source code follows:
_____________________________________
#include <iostream.h>
class Fred { };
class Wilma { };
void
userCode()
throw(Fred)
{
if (rand() % 3)
{ cout << "throwing a Fred; "; throw Fred(); }
if (rand() % 2)
{ cout << "throwing a Wilma; "; throw Wilma(); }
cout << "returning normally; ";
}
void
myUnexpected()
{
cout << "unexpected exception!" << endl;
throw; //rethrow the (unexpected) exception
}
main()
{
//without this, program would silently crash:
set_unexpected(myUnexpected);
for (int i = 0; i < 10; ++i) {
try {
cout << "trying: ";
userCode();
cout << "no exception thrown\n";
}
catch (Fred) {
cout << "caught a Fred\n";
}
catch (Wilma) {
cout << "caught a Wilma\n";
}
catch (...) {
cout << "this should never happen\n";
}
}
}
--
_____________________________________
Andrew McClure <andrew@amac.com.au>
Software Engineer - Wellington NZ
Phone +64 4 479 2002
Mobile +64 21 671 117
_____________________________________