File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S Question on RMI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S Question on RMI" Watch "B&S Question on RMI" New topic
Author

B&S Question on RMI

Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
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

Joined: Oct 29, 2007
Posts: 6
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

Joined: Nov 10, 2007
Posts: 144
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

Joined: Feb 01, 2007
Posts: 77
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

Joined: Mar 12, 2007
Posts: 92

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.


SCJP 1.4, SCJD, OCE EJBD 6
Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
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

Joined: Mar 12, 2007
Posts: 92

I have used the Facade pattern to implement the network functionality
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
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
 
subject: B&S Question on RMI