• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't declare methods to throw java.rmi.RemoteException

 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am implementing RMI code, which is like what Habibi did.
I want to delcare my methods in DBImpl.java to throw RemoteException but I can't.
And it always gives me


suncertify\connector\remoteRMI\DBImpl.java:20: readRecord(long) in suncertify.co
nnector.remoteRMI.DBImpl cannot implement readRecord(long) in suncertify.db.DBCl
ient; overridden method does not throw java.rmi.RemoteException
public String[] readRecord(long recNo) throws RemoteException, RecordNotFoundE
xception{


This exception makes me crazy because I can't find out what's wrong.
Can someone tell me what's wrong in my code?
Many thanks.





[ October 26, 2004: Message edited by: Steve Taiwan ]
[ October 26, 2004: Message edited by: Steve Taiwan ]
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Taiwan:
I am implementing RMI code, which is like what Habibi did.
I want to delcare my methods in DBImpl.java to throw RemoteException but I can't. ...


See my response to this topic.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you cant. This is the rule, which has been examined in SCJP!

If the interface does not declare to throw anException, the class which implements it CANNOT throw that exception as well.

The class can only throw exceptions (or the subclasses of those exceptions) that have been declared to be thrown in its super class, or the interface.

Nick
 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Peter.

Thank you for the reply.
I read your thread. I could do what you said to wrap my own exception into
RemoteException. But in Habibi code, he could declare a RemoteException
for methods of a class which implements Remote interface. But why can't I
do this??? why???
[ October 26, 2004: Message edited by: Steve Taiwan ]
 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Nick.

In Habibi's book, he said "
Any public method defined in the Remote interface must throw a
java.rmi.RemoteException. This is the superclass for all communication
exceptions that occur during the invocation of a remote method."

His code doesn't throw RemoteException. Could you check his code as
the follows???



 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But why can't I do this??? why???

Because Max's dont have the limitation of the interface. In old SCJD, no interface is given by SUN, but now, you have such limit.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

His code doesn't throw RemoteException. Could you check his code as
the follows???

I guess you mix up with some fundemental concept.

You can always declare an interface, or super class to throw anException. But, the subclasses or implementors do NOT require to throw that exception or even throw any exceptions. In case exceptions are required to be thrown, the exception MUST be the subclass of anException. Other combinations are invalid.

Nick
 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Nick.

Please spend a little more time in my question.
I can understand the concept of Exception.
But in Habibi's code, in his DBClient.java and DVDDatabaseRemote, he doesn't
throw any RemoteException. And his DVDDatabaseImpl can throw
RemoteException without compilor error. How could he do it???
 
Matt Sheehan.
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Taiwan:

But in Habibi's code, in his DBClient.java and DVDDatabaseRemote, he doesn't
throw any RemoteException. And his DVDDatabaseImpl can throw
RemoteException without compilor error. How could he do it???


I think it works in Max's code because he throws IOException, which RemoteException is a subclass of.

Matt
 
Anton Golovin
Ranch Hand
Posts: 527
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Steve. In my project, I implement the DB interface in Data; however, then, I hide Data behind a business facade called RoomManager, which implements RoomClient interface. All exceptions in this interface throw the IOException. Then, to enable RMI, I extend the RoomClient and Remote interfaces, getting RemoteRoomClient interface. Then I implement it in the RemoteRoomManager class. As the RemoteException is a subclass of IOException, I can use RemoteException in the methods of the RemoteRoomManager class.

What the code in your example is missing is this intermediate class - RoomManager. I don't think there is a workaround around having to use an Adapter of some sort for this case. In my case, this adapter happens to be the business object; however, if you wish it to be just an adapter, then reimplementing the DBClient interface with methods which throw the IException is a must.
[ October 26, 2004: Message edited by: Anton Golovin ]
 
Steve Taiwan
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All.

Thank you for the reply.

Dear Matt and Anton.

Thank you very much for the answer about IOException. After adding IOException to
my DBClient.java, I can compile DBImpl.java without any error. Finally, I know what
the problem is. Thank you for the solution. Your answer really releases from the hell.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic