gpt4 book ai didi

java - 如何从 web 服务中抛出异常?

转载 作者:行者123 更新时间:2023-12-02 08:25:42 25 4
gpt4 key购买 nike

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

请问有人可以帮忙吗?

最佳答案

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 block 中。

throw new IsCreditCardValidFault();

关于java - 如何从 web 服务中抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4563742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com