File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S Question on RMI

 
Harjit Singh
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to implement an Remote Interface for this application. My interface only is throwing RecordNotFoundException. If I have to expose any method thru the RMI it should actually throw RemoteException. When I add a throws clause to the class which implements the interface, I get an error saying that the exception is not part of the interface. how should I go about this ?

Thanks

- Harjit
 
Peter Hat
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remote methods can only throw RemoteExceptions.

You then have 2 options:
- Wrap your exception in a RemoteException. (Use 'Cause' in the RemoteException)
- For your own Exceptions, you can let them extend the RemoteException, which will allow your class to throw them without breaching the interface.

Hope that helped.
 
R van Vliet
Ranch Hand
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll add that although both of Peter's options are perfectly valid, the first one is very much preferred. (Ab)using RemoteExceptions for an interface that is not necessarily related to RMI is bad style in my opinion.
 
Harjit Singh
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I follow what you are saying. How will I be able to use option 1 with the following

I have a interface which has this method

public String[] readRecord(long recNo) throws RecordNotFoundException;


Thanks

- Harjit
[ November 21, 2007: Message edited by: Harjit Singh ]
 
Romeo Son
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remote methods can only throw RemoteExceptions.


Completly wrong. Remote methods must declare to throw the RemoteException because of the RMI transport implementation. Other than that they can throw any other Exception, cause we all know they are Serializable, didn't we? So no need for any wrapping to propagate checked exceptions from the server to the client.

Hope this helps.
 
Harjit Singh
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that Remote method should throw RemoteException. That is what the question is how would you go ahead and implement this interface without breaking the contract.....

-H
 
Romeo Son
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used the Facade pattern to implement the network functionality
 
R van Vliet
Ranch Hand
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harjit,

If you wish to wrap your exception in a RemoteException as described above do something like :
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic