This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problem with exception handling


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
_____________________________________




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]