g++ exceptions typesafe?

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.
 
I

Ian Collins

Torsten said:
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.

You should be catching const references.

Your code works when compiled with g++ 4.0.2, which version are you using?
 
T

Torsten Mueller

Ian Collins said:
You should be catching const references.

Your code works when compiled with g++ 4.0.2, which version are you
using?

This is really interesting now.

I normally use most likely a 4.3 on opensuse 11 (I don't have it here
at the moment ...) I swear I tried const references to avoid a partial
copy operation from Y to X but it didn't solve the problem.

Now I just tried it with an old 3.4.5 (mingw): this one catches const
references correctly and typesafe.

T.M.
 
T

Torsten Mueller

Torsten Mueller said:
I normally use most likely a 4.3 on opensuse 11 (I don't have it
here at the moment ...) I swear I tried const references to avoid a
partial copy operation from Y to X but it didn't solve the problem.

Now I just tried it with an old 3.4.5 (mingw): this one catches
const references correctly and typesafe.

Ha, it's still different.

My sample program was too easy. My real program uses nested
try-catch-blocks:

try
{
MyClass().Do();
}
catch (MyClass::Y}
{
try
{
MyClass.DoSomethingElse();
}
catch (MyClass::Y)
{
}
}

This nested contruct does indeed not work correctly.

In my case the outer catch handler doesn't catch the Y thrown by Do().
instead the program just terminates with a message - an exception of
type X is thrown - immediately following the throw, this means without
entering the catch handler and even if I add a special catch handler
for real X-exceptions!

But if I remove the (uninvolved!, not executed!) inner try-catch-block
the remaining outer try-catch-block works fine again and catches a
thrown Y correctly. This is mysterious for my taste ...

So I guess: nesting try-catch-blocks is not a good idea on g++.

T.M.
 
I

Ian Collins

Torsten said:
Ha, it's still different.

My sample program was too easy. My real program uses nested
try-catch-blocks:

try
{
MyClass().Do();
}
catch (MyClass::Y}
{
try
{
MyClass.DoSomethingElse();
}
catch (MyClass::Y)
{
}
}

This nested contruct does indeed not work correctly.

In my case the outer catch handler doesn't catch the Y thrown by Do().
instead the program just terminates with a message - an exception of
type X is thrown

You don't catch X or show what DoSomethingElse() does in the code you
posted. Post a real example (preferably without tabs!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top