Multiple Inheritance

B

Brian C

Hello all,
I read through the C++ FAQ and I think I'm good here. I'm posting to
see if there may be other ways that I'm not thinking of.
What I've got is a cross-platform library I've developed, and use to
develop applications on both Linux, AIX & Windows. Now, some things I
have in there are my own exceptions. One exception I have is a
"ConnectionFailed" exception which currently extends a
"NetworkException". This way I can catch just a NetworkException when I
don't care what kind of network exception occurred.
I'm adding a MySQL wrapper class, and of course you need to connect
into the database. I wanted to use the same "ConnectionFailed" exception
instead of making a "DatabaseConnectionFailed" exception (or the like).
So, I created a generic "DatabaseException" and had ConnectionFailed
extend both the Network & DatabaseException classes.
Is this an accepted method? If so/if not, anyone got any other ideas?
Again, I don't want to make seperate "SocketConnectionFailedException"
and "DatabaseConnectionFailedException" classes.

Thanks.
 
V

Victor Bazarov

Brian said:
Hello all,
I read through the C++ FAQ and I think I'm good here. I'm posting to
see if there may be other ways that I'm not thinking of.
What I've got is a cross-platform library I've developed, and use to
develop applications on both Linux, AIX & Windows. Now, some things I
have in there are my own exceptions. One exception I have is a
"ConnectionFailed" exception which currently extends a
"NetworkException". This way I can catch just a NetworkException when
I don't care what kind of network exception occurred.
I'm adding a MySQL wrapper class, and of course you need to connect
into the database. I wanted to use the same "ConnectionFailed"
exception instead of making a "DatabaseConnectionFailed" exception
(or the like). So, I created a generic "DatabaseException" and had
ConnectionFailed extend both the Network & DatabaseException classes.
Is this an accepted method? If so/if not, anyone got any other ideas?
Again, I don't want to make seperate "SocketConnectionFailedException"
and "DatabaseConnectionFailedException" classes.

Seems OK. Now, make sure that when you write your 'catch' clauses,
you put more important one first. Here is an example:

#include <iostream>
class DBx {};
class NETx {};
class Cx : public NETx {};
class DBConnX : public Cx, public DBx {};

void foo()
{
throw DBConnX();
}

int main()
{
try {
foo();
}
catch (Cx & cx) {
std::cout << "connection exception" << std::endl;
}
catch (DBx & dbx) {
std::cout << "database exception" << std::endl;
}
catch (...) {
// who knows?
}

try {
foo();
}
catch (DBx & dbx) {
std::cout << "database exception" << std::endl;
}
catch (Cx & cx) {
std::cout << "connection exception" << std::endl;
}
catch (...) {
// who knows?
}
}

It prints
 
B

Brian C

Victor said:
Brian C wrote: ::snip::
Seems OK. Now, make sure that when you write your 'catch' clauses,
you put more important one first. Here is an example:
::snip::
Victor,
I definitely will be aware of that. Typically though, I will be
handling the "NetworkException" and "DatabaseException" where they are
actually expected, not in say something like main(). If I forget to
(probably of course right?) I'll fix it.
The layout is actually more like this:

class NetX() { };
class DBX() { };
class Cx() : public NetX, public DBX

There is no DBConnX(). Cx() takes both, depending on the type of
connection you're attempting.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top