Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Are so many subclasses such a good idea?

Reply
Thread Tools

Are so many subclasses such a good idea?

 
 
Erik Wikström
Guest
Posts: n/a
 
      06-13-2008
On 2008-06-13 00:26, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Jun 12, 5:49 pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>
>> And where is the _selective_ catching of errors?

>
> Here:
> try {
> if (a)
> throw(UNHAPPY);
> if (b)
> throw(UNHAPPIER);
> if (c)
> throw(UNHAPPIEST);
> } catch (int which) {
> switch (which) {
> case UNHAPPY: ... break;
> case UNHAPPIER: ... break;
> case UNHAPPIEST: ... break;
> }
> }


What if not all errors can be handled at the same place:

class e1;
class e2;
class e3;

void f()
{
try {
if (a) throw e1;
if (b) throw e2;
if (c) throw e3;
}
catch (e1) {
// ...
}
}

void g()
{
try {
f();
}
catch (e2) {
// ....
}
}

int main()
{
try {
g();
}
catch (e3) {
// ...
}
}

Can not be solved with an int unless you rethrow those that can not be
handled.

--
Erik Wikström
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      06-13-2008
(E-Mail Removed) schrieb:
> On Jun 12, 5:49 pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>
>> And where is the _selective_ catching of errors?

>
> Here:
> try {
> if (a)
> throw(UNHAPPY);
> if (b)
> throw(UNHAPPIER);
> if (c)
> throw(UNHAPPIEST);
> } catch (int which) {
> switch (which) {
> case UNHAPPY: ... break;
> case UNHAPPIER: ... break;
> case UNHAPPIEST: ... break;
> }
> }


How to boundle additional information with the exception, like the
filename of the file that couldn't be opened, or the IP-Address of the
connection attempt that didn't worked out?

Using a class as exception object, you can attach valuable information
to the exception.

>> The point is: different exceptions can rise up in the call stack to
>> different levels. The type determines which catch handler is used.

>
> True, that reinforces C++'s compile-time typing,
> which some consider to be an advantage of C++.


And C++'s run-time type information. You don't know until run-time,
which type of exception will be thrown.

--
Thomas
 
Reply With Quote
 
 
 
 
khapi@yahoo.com
Guest
Posts: n/a
 
      06-22-2008

> What if not all errors can be handled at the same place:

....
>
> Can not be solved with an int unless you rethrow those that can not be
> handled.


Why rethrow? One can just create a centralized
handler function.

void central_handler(int e)
{
...
}
void func() {
try {
something();
}
catch (int e) {
central_handler(e);
}
}
void func2() {
try {
something2();
}
catch (int e) {
central_handler(e);
}
}

And for functions that require immediate
action that can be done in the catch.
 
Reply With Quote
 
khapi@yahoo.com
Guest
Posts: n/a
 
      06-22-2008
On Jun 13, 9:15*am, Bart van Ingen Schenau
<(E-Mail Removed)> wrote:
> And if you chose to handle all io-errors
> identically at some level, you don't even have to know that there are
> more specific types below that level, nor do you have to change
> anything when someone adds a write-error to the category of file-
> errors.
>
> Try doing that with numeric error codes.


OK, I agree that hierarchies of exception classes
could be useful. I was using that as a concrete example
however and my complaint was intended more generally
i.e. deep class inheritance trees are used for
more than just exceptions.



 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      06-22-2008
On 22 Jun., 15:22, (E-Mail Removed) wrote:
> > What if not all errors can be handled at the same place:

> ...
>
> > Can not be solved with an int unless you rethrow those that can not be
> > handled.

>
> Why rethrow? One can just create a centralized
> handler function.
>
> void central_handler(int e)
> {
> *...}
>
> void func() {
> * try {
> * * *something();
> * }
> * catch (int e) {
> * * *central_handler(e);
> * }}
>
> void func2() {
> * try {
> * * *something2();
> * }
> * catch (int e) {
> * * *central_handler(e);
> * }
>
> }
>
> And for functions that require immediate
> action that can be done in the catch.


But that central error-handler would probably only be central for f. I
assume that your program is not of the type that every error is
handled in the same manner, no matter the context. If this is the
case, your program would be quite unusual.

/Peter
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-22-2008
(E-Mail Removed) wrote:
> On Jun 13, 9:15 am, Bart van Ingen Schenau
> <(E-Mail Removed)> wrote:
>> And if you chose to handle all io-errors
>> identically at some level, you don't even have to know that there are
>> more specific types below that level, nor do you have to change
>> anything when someone adds a write-error to the category of file-
>> errors.
>>
>> Try doing that with numeric error codes.

>
> OK, I agree that hierarchies of exception classes
> could be useful. I was using that as a concrete example
> however and my complaint was intended more generally
> i.e. deep class inheritance trees are used for
> more than just exceptions.
>
>
>


In general, on way that subclassing is useful in that it provides
polymorphic behavior. It should almost always be preferred to create a
new Type, than a Type-Code.

Warning: Generalization ahead, take with a grain of salt...

Any time one has a value that is constant for the lifetime of an object
and affects the behavior of an object, one should attempt to convert
that into a polymorphic object instead. The leads to less
"if/then/else" and "switch/case" statements that become more and more
unmaintainable over time.

Just my 2 cents.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a good way to implement file updating? such as lock, update,reload... Evan Python 2 07-07-2008 01:40 PM
is such exception handling approach good? George2 C++ 3 12-24-2007 09:53 AM
Hibernate problem with too many subclasses Jacqui Java 0 01-04-2006 04:17 PM
Duplicate declaration of member Locale (many such messages) =?Utf-8?B?TWljaGVsbGUgU3RvbmU=?= ASP .Net 0 04-25-2004 09:06 PM



Advertisments