简体   繁体   English

如何从Web服务引发异常?

[英]How can I throw exception from webservice?

I am using netbeans to make webservices, I want to make composite webservice using PBEL, I face a problem in throwing exception in each service, I define complex Type in the schema of the exception I want to throw, and I make it in WSDL too , but inside the service I don't know how can I throw the exception , Here's the example I am working on : 我正在使用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 .
}

Please can Someone help? 请有人帮忙吗?

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

needs to be written as 需要写成

throw new IsCreditCardValidFault();

throws is used in your declaration of the method, where the throw keyword is used inside the method to indicate where you will throw the exception. throws用于您的方法声明,其中throw关键字在方法内使用,以指示您将抛出异常的位置。

so as an example 举个例子

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

but in your case, you want to initiate the exception yourself so you have to create a new object of that exception type. 但在您的情况下,您希望自己启动异常,因此您必须创建该异常类型的新对象。 You will create the exception yourself, so no need to enclose in a try/catch block. 您将自己创建异常,因此不需要包含在try / catch块中。

throw new IsCreditCardValidFault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM