Mitja Rogl
Mitja Rogl

Reputation: 884

returning null or throw exception

I'm developing a simple web service and client. See my snippet of service:

 @webService
 public Car carData(int id)  {
   try {
    return carDAO.getCars(id);
    } catch (NullPointerException e) {
        System.out.println("Error: "+e.getLocalizedMessage());

    }
    return  null;

}

Is is ok to returning null to client, and then the client should take care of null OR is it better that web service take care of null and throws exception?

UPDATE: How can I return nullpointerexception instead of returning null?

What would you recommend?

Thanks

Upvotes: 0

Views: 1483

Answers (3)

Paul Vargas
Paul Vargas

Reputation: 42020

You can use SOAP faults in JAX-WS Web Services. This way, you can throw a custom exception when the prerequisites are not found. The client has a better idea of what happened.

For that, you need an Exception with @WebFault annotation. You can find a good example in Using SOAP Faults and Exceptions in Java JAX-WS Web Services - Eben Hewitt on Java.

In the Users Guide » GlassFish » Metro » JAX-WS you can find this example:

package fromjava.server;

import javax.jws.WebService;

@WebService
public class AddNumbersImpl {
    /**
     * @param number1
     * @param number2
     * @return The sum
     * @throws AddNumbersException if any of the numbers to be added is 
     * negative.
     */
    public int addNumbers(int number1, int number2) throws 
            AddNumbersException {
        if (number1 < 0 || number2 < 0) {
            throw new AddNumbersException("Negative number cant be " +
                    "added!", "Numbers: " + number1 + ", " + number2);
        }
        return number1 + number2;
    }
}

The Exception:

package fromjavahandler.server;

public class AddNumbersException extends Exception {

    String detail;

    public AddNumbersException(String message, String detail) {
        super(message);
        this.detail = detail;
    }

    public String getDetail() {
        return detail;
    }
}

The JAX-WS runtime generate the Fault automatically.

Upvotes: 1

Bohemian
Bohemian

Reputation: 425003

You shouldn't be getting an NPE. if you do, it probably means your service isn't initialized and you should probably throw an IllegalStateException or reply with a message back to the client to say that the web service is unavailable

Upvotes: 0

CodeBlind
CodeBlind

Reputation: 4569

There's probably going to be a million different opinions on this, but I'll tell you what I'd do.

Typically remote services are already capable of throwing IOException when remote calls are made and subsequently fail, so I like to try to spare my clients the possibility of ever having to deal with RuntimeException or any of its subclasses (which includes NullPointerException). If your service throws a RuntimeException and the client prints out the stack trace, it's confusing to the client-side developer because the stack trace pertains to code running on the server that the developer may have little or no control over. Ideally, the client should only have to handle cases where null was returned instead of the expected response. Only throw an Exception on the server side if null is not sufficient to tell the client that something went wrong or if the problem stems from an issue with the inputs supplied by the client.

Upvotes: 0

Related Questions