# [SOLVED] [C++] Exceptions - Catch clause for base class doesn't catch derived ...

This topic is 4681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have an exception class hierarchy like this (simplified):
class ExceptionBase
{
protected:
ExceptionBase( const ExceptionBase& rSource )
{
// ...
}

ExceptionBase()
{
// ...
}

public:
virtual ~ExceptionBase()
{
// ...
}

virtual void raise() = 0;

};

class DerivedException : public ExceptionBase
{
public:
DerivedException( const DerivedException& rSource ) : ExceptionBase( rSource )
{
// ...
}

DerivedException()
{
// ...
}

virtual ~DerivedException()
{
// ...
}

virtual void raise()
{
throw *this;
}
};


Now from all I know about exception handling, a catch clause for class ExceptionBase should also catch objects of class DerivedException, right? At least if I catch by reference, which I do. However, this is not what's happening in my program:
int func()
{
try
{
DerivedException d;

d.raise();
}
catch ( const ExceptionBase& rException )
{
std::cerr << "ExceptionBase caught" << std::endl;

exit( 1 );
}
catch ( ... )
{
std::cerr << "Unkown exception caught!!" << std::endl;

exit( 1 );
}

return 0;
}

int main()
{
return func();
}

// Outputs: Unkown exception caught!!


Why?? =( I'm not getting it! I'm sure it's just stumbling over some stupid mistake again, but I just can't find it! [Edited by - Red Ant on January 26, 2006 8:27:44 AM]

##### Share on other sites
I'm not sure about, but: You're using a stack variable as an exception. At the moment of catching the scope of "try" is left, and hence the exception's object d is removed! I don't assume the compiler to have made a copy before, but perhaps I'm wrong.

I personally use only pointers to objects for catching exceptions (EDIT 2: but will no longer do so; see below). However, seen from the type system your solution should work, I think.

EDIT: gcc catches the exception regardless of whether its catch clause has a const qualified or a non-const qualified exception object specified.

Interestingly gcc also complains a private copy c'tor in the base class of exception, in the line of throw. So it seems that it actually does a copy!

[Edited by - haegarr on January 26, 2006 5:12:43 AM]

##### Share on other sites
I just compiled that code in MSVC 6.0 and it caught the ExceptionBase just as it should. What compiler are you using?

 haegarr's point makes sense, I guess my compiler just optimized the code differently

##### Share on other sites
Works fine for me, using MinGW 3.4.4.
Which compiler did you use ?

Quote:
 Original post by haegarrI'm not sure about, but: You're using a stack variable as an exception. At the moment of catching the scope of "try" is left, and hence the exception's object d is removed! I don't assume the compiler to have made a copy before, but perhaps I'm wrong.

In function raise() the object is thrown by value:
"return *this;"
Catching by reference avoids unnecessary creation of copies.

Quote:
 I personally use only pointers to objects for catching exceptions. However, seen from the type system your solution should work, I think. Or does also play the const keyword a role here?

http://www.parashift.com/c++-faq-lite/exceptions.html
According to the FAQ, catching by pointers is not recommended.

##### Share on other sites
Quote:
 Original post by haegarrI'm not sure about, but: You're using a stack variable as an exception. At the moment of catching the scope of "try" is left, and hence the exception's object d is removed! I don't assume the compiler to have made a copy before, but perhaps I'm wrong.I personally use only pointers to objects for catching exceptions. However, seen from the type system your solution should work, I think. Or does also play the const keyword a role here?

Catching exception by reference is the preferred way of doing it.

##### Share on other sites
I had a very similar problem to this a while back on an old compiler for VXWork (circa '95). If I remember correctly, I ended up having my base class inheriting from std::exception and this, for some odd reason, made my code behave as expected. I don't have any better explanation other than it just worked. Since we're not in VXWord anymore, I took it out again and have it like you do right now and it works in vs6, vs7.1 and gcc 3.2+(?) on both linux and windows.

##### Share on other sites
Hello all,

I'm using the MSVC 7.1 compiler. The silly thing is, I recall this working correctly in an early test project I made a while ago, but now in my actual development project I get this funny behavior. I suppose I should make another test project and see what happens.

##### Share on other sites
I see from the site cited by nmi that pointers aren't good for exceptions due to memory handling. Although it isn't a problem in my software anyway, I accept that it is better to use references instead. So I have to do some more re-programming now :)

However, as already stated in an edit of my post above, it actually seems that a copy of the exception object is made, else the compiler wouldn't have to complain a private copy c'tor. So the question is left: what's wrong within the OP?

##### Share on other sites
Quote:
 Original post by ShadxI had a very similar problem to this a while back on an old compiler for VXWork (circa '95). If I remember correctly, I ended up having my base class inheriting from std::exception and this, for some odd reason, made my code behave as expected.

I omitted this detail for the sake of simplicity, but in fact my exception base is in turn derived from std::exception.

##### Share on other sites
Quote:
 Original post by Red AntThe silly thing is, I recall this working correctly in an early test project I made a while ago, but now in my actual development project I get this funny behavior.

So this code is part of your actual project ?
Since this code works fine the error must be in another part of your project.
Do you throw exceptions from destructors or something like that ?
Do you rethrow exceptions ?

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011702
• ### Who's Online (See full list)

There are no registered users currently online

×