T
Torsten Mueller
I wonder how typesafe g++ exceptions are. I want to implement a local
exception hierarchy as shown in this little program:
class MyClass {
public:
class X {};
class Y: public X {};
MyClass() {}
void Do () throw (X) {throw Y();}
};
int main (int arc, char* argv[])
{
try
{
MyClass().Do();
}
catch (MyClass::Y)
{
}
catch (MyClass::X)
{
}
return 0;
}
If I throw Y I always catch only X, never Y. I tried several things:
virtual method in X (destructor), catching references and const
references and more. What I detected is: if at least X is defined
_outside MyClass_ everything works. Is this really intended this way?
Why are nested classes less typesafe?
T.M.
exception hierarchy as shown in this little program:
class MyClass {
public:
class X {};
class Y: public X {};
MyClass() {}
void Do () throw (X) {throw Y();}
};
int main (int arc, char* argv[])
{
try
{
MyClass().Do();
}
catch (MyClass::Y)
{
}
catch (MyClass::X)
{
}
return 0;
}
If I throw Y I always catch only X, never Y. I tried several things:
virtual method in X (destructor), catching references and const
references and more. What I detected is: if at least X is defined
_outside MyClass_ everything works. Is this really intended this way?
Why are nested classes less typesafe?
T.M.