palAlaa
palAlaa

Reputation: 9858

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 :

@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?

Upvotes: 0

Views: 4133

Answers (1)

Sean
Sean

Reputation: 7737

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.

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.

throw new IsCreditCardValidFault();

Upvotes: 2

Related Questions