0

我正在使用 netbeans 制作 web 服务,我想使用 PBEL 制作复合 web 服务,我在每个服务中都遇到了抛出异常的问题,我在要抛出的异常的架构中定义了复杂的类型,我也在 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 .
}

请问有人可以帮忙吗?

4

1 回答 1

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();
于 2010-12-30T15:29:04.967 回答