La gestion des exceptions utilisateur au sein d’un webservice utilisant l’API JAX-WS doit respecter quelques contraintes afin de  transmettre l’ensemble des informations au client SOAP.

Voici un exemple pour une classe d’exception UserFault dont les détails optionnels sont implémentés par la classe UserFaultDetail :

@WebFault
public class UserFault extends Exception {
	private static final long serialVersionUID = 1L;
	private UserFaultDetail userFaultDetail;

	/**
	 * Constructs an <code>UserFault</code> object.
	 *
	 * @param message
	 *            the error message.
	 * @param userFaultDetail
	 *            the error details.
	 */
	public UserFault(String message, UserFaultDetail userFaultDetail) {
		super(message);
		this.userFaultDetail = userFaultDetail;
	}

	/**
	 * Constructs an <code>UserFault</code> object.
	 *
	 * @param message
	 *            the error message.
	 * @param userFaultDetail
	 *            the error details.
	 * @param cause
	 *            the error cause.
	 */
	public UserFault(String message, UserFaultDetail userFaultDetail,
			Throwable cause) {
		super(message, cause);
		this.userFaultDetail = userFaultDetail;
	}

	/**
	 * Returns the error details.
	 *
	 * @return the error details.
	 */
	public UserFaultDetail getFaultInfo() {
		return userFaultDetail;
	}
}
public class UserFaultDetail {
	public static final int ERROR_UNKNOWN = 0;
	public static final int ERROR_INVALIDAPIKEY = -1;
	private int code;

	/**
	 * Constructs a new <code>UserFaultDetail</code> object.
	 */
	public UserFaultDetail ()
	{
		this.code = UserFaultDetail.ERROR_UNKNOWN;
	}

	/**
	 * Constructs a new <code>UserFaultDetail</code> object.
	 * @param code the code of the fault.
	 */
	public UserFaultDetail (int code)
	{
		this.code = code;
	}

	/**
	 * Returns the code of the fault.
	 * @return the code.
	 */
	public int getCode() {
		return code;
	}

	/**
	 * Sets the code of the fault.
	 *
	 * @param code
	 */
	public void setCode(int code) {
		this.code = code;
	}
}