我试图在下面的代码中抛出超时异常。我尝试了一个简单的条件,但这不是正确的方法。 我的问题是如何区分超时异常和 SOAPException?
URL endpoint = new URL(null,
urlStr,
new URLStreamHandler() {
// The url is the parent of this stream handler, so must create clone
protected URLConnection openConnection(URL url) throws IOException {
URL cloneURL = new URL(url.toString());
HttpURLConnection cloneURLConnection = (HttpURLConnection) cloneURL.openConnection();
// TimeOut settings
cloneURLConnection.setConnectTimeout(10000);
cloneURLConnection.setReadTimeout(10000);
return cloneURLConnection;
}
});
try {
response = connection.call(request, endpoint);
} catch (SOAPException soapEx) {
if(soapEx.getMessage().contains("Message send failed")) {
throw new TimeoutExpirationException();
} else {
throw soapEx;
}
}
最佳答案
以下几行来自call
方法的开放jdk源代码。在代码中,它们仅捕获Exception
(也带有链接?注释)。我认为没有其他方法,除非 Oracle jdk 以不同的方式处理这个问题。
您仍然可以尝试类似 if(soapEx.getCause() instanceof SomeTimeoutException)
(不确定这是否有效)
try {
SOAPMessage response = post(message, (URL)endPoint);
return response;
} catch (Exception ex) {
// TBD -- chaining?
throw new SOAPExceptionImpl(ex);
}
如果你想查看源码HttpSoapConnection
关于java - 从 SOAPException 抛出超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48909037/