簡體   English   中英

如何從Web服務引發異常?

[英]How can I throw exception from webservice?

我正在使用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 .
}

請有人幫忙嗎?

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM