Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A question about Exception inheritance

Reply
Thread Tools

A question about Exception inheritance

 
 
www
Guest
Posts: n/a
 
      10-18-2007
Hi,

I have three classes(Animal, Dog and Cat). For them, I have created
three kinds of Exception class too:

AnimalException, DogException and CatException.

Animal is the superclass of Dog and Cat.

AnimalException, a subclass of Exception, is the superclass of
DogException and CatException.

public class Animal
{
public void doIt() throws AnimalException
{
if(..)
{
throw new AnimalException(getClass().getName() + " screwed up.");
}
}
}


public class Dog extends Animal
{
public void doIt() throws DogException
{
super.doIt(); //oops, wrong!
}
}

The message says that the AnimalException is not handled. I understand
it. So I have to do it:

public class Dog extends Animal
{
public void doIt() throws DogException
{
try
{
super.doIt();
}
catch(AnimalException e)
{
throw new DogException(e.getMessage()); //take the message, re-throw
it again.
}
}
}

I feel this is really pain and un-necessary. Since I run into such
patterns many times, I am wondering if anybody could help me out.

Is that wrong to have DogException and CatException? Should I just keep
AnimalException and replace DogException with AnimalException?

Thank you very much.
 
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
Exception + multiple inheritance question... barcaroller C++ 5 07-19-2009 09:09 PM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
A question about Exception inheritance www Java 5 11-12-2007 05:11 PM
A question about Exception inheritance www Java 0 10-18-2007 06:01 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM



Advertisments