我正在使用带有XML bean的Axis2设置Web服务客户端。 我的代码退出我的calculateTest函数而没有抛出异常。如果您查看下面的代码,代码将退出" AuthHeader authHeader = authHeaderDocument.addNewAuthHeader();"
public class ClientTest {
private static final int IDLE_CONNECTION_TIME = 0;
private ServiceStub serviceStub;
private MultiThreadedHttpConnectionManager connectionManager;
public ServiceStub getServiceStub() {
return serviceStub;
}
public void setServiceStub(ServiceStub serviceStub) {
this.serviceStub = serviceStub;
}
public static void main(String[] args) {
try {
System.out.println("STARTING");
ClientTest a = new ClientTest();
a.initServiceClient("https://XXXXXXX.asmx?WSDL");
a.calculateTest();
System.out.println("FINISHED");
} catch (Exception e) {
e.printStackTrace();
}
}
public void initServiceClient(String targetEndpoint) throws Exception {
try {
org.apache.axis2.context.ConfigurationContext myConfigContext = org.apache.axis2.context.ConfigurationContextFactory.createDefaultConfigurationContext();
this.connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setStaleCheckingEnabled(true);
params.setDefaultMaxConnectionsPerHost(100);
this.connectionManager.setParams(params);
HttpClient httpClient = new HttpClient(this.connectionManager);
myConfigContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
myConfigContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Boolean.TRUE);
if(targetEndpoint != null){
serviceStub = new DACALCServiceStub(myConfigContext,targetEndpoint);
}else{
serviceStub = new DACALCServiceStub(myConfigContext);
}
ServiceClient sc = serviceStub._getServiceClient();
Options options = sc.getOptions();
options.setProperty(HTTPConstants.SO_TIMEOUT, 30000);
options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, 30000);
options.setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String calculateTest() throws Exception {
System.out.println("Inside calculateTest");
String output = null;
try {
System.out.println("1.");
String authenticationUsername = "XXXX";
String authenticationPassword = "XXXX";
String errorMsg = "";
System.out.println("1a.");
AuthHeaderDocument authHeaderDocument = AuthHeaderDocument.Factory.newInstance();
System.out.println("1a1.");
AuthHeader authHeader = authHeaderDocument.addNewAuthHeader();
System.out.println("1a2.");
authHeader.setUserName(authenticationUsername);
System.out.println("1a3.");
authHeader.setPassword(authenticationPassword);
System.out.println("1a4.");
authHeader.setErrorMessage(errorMsg);
System.out.println("1b.");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
serviceStub._getServiceClient().cleanupTransport();
this.connectionManager.closeIdleConnections(IDLE_CONNECTION_TIME);
return output;
}
}
}
我看到的输出是:
STARTING
Inside calculateTest
1.
1a.
FINISHED
不会抛出异常。为什么代码会在此行上无声地失败?
答案 0 :(得分:0)
我在calculateTest中删除了try / catch并抛出了异常(奇怪的是它没有被通用的异常处理程序捕获)。 事实证明,我没有在我的资源目录中包含一些必需的文件。一旦我补充说,一切都有效。
答案 1 :(得分:0)
我想通过“退出而不抛出异常”,你实际上意味着退出而不执行catch块中的代码。解释是您正在捕捉Exception
,而不是Throwable
。这意味着不会捕获扩展Error
的异常。