Catching exceptions by reference

Matthias Seeger mattseeger@netscape.net
Wed Mar 21 08:26:00 GMT 2001


Hello,

in Stroustrups 3rd edition (C++ programming language), p.360, he claims
that exceptions can be caught by reference (example below). This would
be very useful for exception class hierarchies.

class a {
public:
  void f() {
    cout << "A" << endl;
  }
};

class b : public a {
public:
  void f() {
    cout << "B" << endl;
  }
};

main()
{
  try {
    throw b();
  } catch (a& ex) {
    ex.f();
  } catch (...) {
    cout << "Caught s.th. else" << endl;
  }
}

The output here is "A" instead of "B", as claimed by Stroustrup. Even
worse, if "catch (a& ex)" is replaced by "catch(a* ex)", the output is
"Caught s.th. else".

Is anything like catching exceptions by reference realized in GCC?

Thanks,
__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/



More information about the Gcc-help mailing list