• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what Exception should be thrown in DataInterface

 
Wendell Su
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public interface DataInterface{
...
public void add(String [] newData) throws ???;
....
}
public interface RemoteDataInterface extends Remote,DataInterface{
...
}
??? is DataBaseException or RemoteException
 
dean tomlinson
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both - your gui layer should deal only with your datainterface, it needn't know anything about remotedatainterface.
[ June 05, 2002: Message edited by: dean tomlinson ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Wendell"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!

In my submission the first interface just called DataAccess throws Exception
In my two implementations DataAccessLocal throws DatabaseException and DataAccessRemote throws RemoteException and DatabaseException
Mark
 
dean tomlinson
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By keeping an interface generic ie. throwing Exception from methods, and taking Object type method parameters and returning Object's, you are maximising the interfaces re-use potential. This is because it is totally indpendent of any specifc implementation classes or exceptions.
the only problem with this is if you build your client to work against such an interface, your client end's up catching Exception instead of the specifc exception's that are thrown by the implementations such as DatabaseException (if there was an invalid recrod number), or RemoteException if the network connection failed.
Therefore the interface has given the client no way of handling these to actual exception types seperately.
If the interface we worked against was declared to throw Exception, that would mean the client could only be written to catch Exception.
I think this not recommended, becasue you would catch any runtime exceptions, and as a rule these shouldn't be caught - so the user has visibility of program bugs - and can report them.

[ June 05, 2002: Message edited by: dean tomlinson ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you implement the interface, you should always throw more specific Exceptions, so that your calling code does not have to catch Exception, but can be more specific, if needed.
Sometimes you just want to catch Exception because any kind of exception that could be thrown are all handled exactly the same.
I find that only when you want to handle each specific type differently do you need to catch the specific exceptions.
But of course that is my opinion only.
Mark
 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
Me too did the same way (ie DataInterface methods throws Exception ), If i try to call any remote method form the client side instred of asking to handle the RemoteException, It asks to handle the Exception.
To avoid this i have plan to change as follows please give me ur valid feed back.

DataInterface extends Remote
{

/*has all the public methods of
Data class */

}
In the above interface, methods will not throw any Exception, The above interface will be implemented by both the localImpl,remoteImpl. RemoteImpl will extend UnicastRemoteObject and all the remote methods will throw RemoteException. Incase of localImpl the methods will throw apprapriate exception.
Guys please feel free to give ur valiable feedback.
regards,
-rameshkumar
 
Robin Underwood
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about throwing IOException and DatabaseException? IOException indicates some sort of generic I/O problem, and is the parent exception of RemoteException. I thought this was generic enough to allow the interface to be used beyond RMI, but also gives some guidance of how the interface should work (throws exceptions related to IO/communications problems and database problems).
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can see what my opinion was in my first post in this thread.
Mark
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
In my two implementations DataAccessLocal throws DatabaseException and DataAccessRemote throws RemoteException and DatabaseException

So are they both effectively a proxy for Data? I considered making Data implement Remote, and having a proxy for local hide the differences, but decided this would entail more hacking of the existing Data API than Sun might like to see. Creating a common interface makes sense for the two proxies make sense. Has anybody considered modifying the 'class Data ...' declaration to also implement the interface? It might make creating the proxies a bit more flexible.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOme people take their DataAccess interface and have the Data class implement it, which in reality it already does, but wouldn't be castable.
This way you can cast the Data class to DataAccess.
Personally I think that is like trying to put the hardwood floors under the carpet, after the carpet is there.
Now you need a floor under carpet in real life, but it's like slipping something under the paper, that isn't necessary.
Mark
 
Wendell Su
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
I have changed my displayed name!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic