如何从webservice中抛出异常?

时间:2010-12-30 15:17:16

标签: java web-services exception throw

我使用netbeans制作webservices,我想使用PBEL制作复合web服务, 我在每个服务中抛出异常都遇到了问题,我在我要抛出的异常的模式中定义了复杂的Type,我也在WSDL中创建它,但是在服务内部我不知道如何抛出异常,这是我正在研究的例子:

@WebService(serviceName = "CreditCardService", portName = "CreditCardPort", endpointInterface = "org.netbeans.j2ee.wsdl.creditcard.CreditCardPortType", targetNamespace = "http://j2ee.netbeans.org/wsdl/CreditCard", wsdlLocation = "WEB-INF/wsdl/NewWebServiceFromWSDL/CreditCard.wsdl")
public class NewWebServiceFromWSDL implements CreditCardPortType {

public org.netbeans.xml.schema.creditcard.CreditCardResponseType isCreditCardValid(org.netbeans.xml.schema.creditcard.CreditCardType creditCardInfoReq) throws IsCreditCardValidFault {

    List<CreditCardType> creditCards = parseCreditCardsFile();
    CreditCardResponseType creditCardResponseElement = new CreditCardResponseType();

    for (CreditCardType aCreditCard : creditCards) {

        if (creditCardInfoReq.getCreditCardNo() == Long.parseLong(String.valueOf(aCreditCard.getCreditCardNo())) {
            creditCardResponseElement.setValid(true);
            return creditCardResponseElement;
        }
    }
    throws  IsCreditCardValidFault();   //here I want to throw an exception .
}

请有人帮忙吗?

1 个答案:

答案 0 :(得分:2)

throws  IsCreditCardValidFault();   //here I want to throw an exception .

需要写成

throw new IsCreditCardValidFault();

throws用于您的方法声明,其中在方法中使用throw关键字来指示您将抛出异常的位置。

以示例

try {
   //do something which generates an exception
}catch(Exception e){
   throw e;
}

但在您的情况下,您希望自己启动异常,因此您必须创建该异常类型的新对象。您将自己创建异常,因此无需将其包含在try / catch块中。

throw new IsCreditCardValidFault();