views:

309

answers:

2

I am writing a web service. So I have:

public interface MyService extends Remote
{
  public void service1();
}

public class MyServiceImpl implements MyService
{
  public void service1() { /* do something that sometimes throws an exception */ }
}

I was reading about RemoteException. Should I wrap all the code in service1() in a try..catch that wraps any exception in a RemoteException? Then I will have to do it for service2(), service3() and so on.

Is it better to let the invoker (a servlet in my case) to do this?

What exactly should be wrapped in remote exception - everything that happens in the server? or only exceptions dealing with the remote invocation process (reflection, serialization, etc)?

A: 

As far as I know you only need to declare that the methods throw RemoteException in the interface.

public interface MyService extends Remote {

public void service1() throws RemoteException;

}

Tom Woolfrey
+1  A: 

Your code shouldn't be throwing RemoteException, it should be reserved for the WS framework if and when errors occur accessing the remote service.

Most of the frameworks will catch Exception, wrap it up as a SOAP-Fault, which is the client deals with normally by throwing another exception with the error message from the SOAP-Fault (remember the client may not be Java).

The up-shot of this is you should throw exceptions specific to the fault that has occurred, leaving the framework to send the appropriate response.

Nick Holt
Wow, I somehow missed your answer until now. Sounds as if I've been doing it all wrong...Thanks for the info.
Doron Tohar