Java 在JAX-WS Web服务调用中捕获ConnectException
我使用JAX-WS2.2.5框架来调用Web服务。我想确定由于Web服务关闭或无法访问而导致调用失败的特殊情况 在一些调用中,我得到一个WebServiceExceptionJava 在JAX-WS Web服务调用中捕获ConnectException,java,web-services,exception-handling,jax-ws,webservices-client,Java,Web Services,Exception Handling,Jax Ws,Webservices Client,我使用JAX-WS2.2.5框架来调用Web服务。我想确定由于Web服务关闭或无法访问而导致调用失败的特殊情况 在一些调用中,我得到一个WebServiceException catch(javax.xml.ws.WebServiceException e) { if(e.getCause() instanceof IOException) if(e.getCause().getCause() instanceof ConnectExcep
catch(javax.xml.ws.WebServiceException e)
{
if(e.getCause() instanceof IOException)
if(e.getCause().getCause() instanceof ConnectException)
// Will reach here because the Web Service was down or not accessible
在其他地方,我得到ClientTransportException(从WebServiceException派生的类)
捕获此错误的好方法是什么
我应该用像这样的东西吗
catch(javax.xml.ws.WebServiceException e)
{
if((e.getCause() instanceof ConnectException) || (e.getCause().getCause() instanceof ConnectException))
{
// Webservice is down or inaccessible
或者有更好的方法吗?首先,您必须确定要捕获的顶级
异常。正如您所指出的,这里是webserviceeexception
接下来要做的是,如果getCause()
返回null
,则更一般地避免NullPointerException
catch(javax.xml.ws.WebServiceException e)
{
Throwable cause = e;
while ((cause = cause.getCause()) != null)
{
if(cause instanceof ConnectException)
{
// Webservice is down or inaccessible
// TODO some stuff
break;
}
}
}
也许你也会想处理未知的后异常
Throwable cause = e.getCause();
while (cause != null)
{
if (cause instanceof UnknownHostException)
{
//TODO some thing
break;
}
else if (cause instanceof ConnectException)
{
//TODO some thing
break;
}
cause = cause.getCause();
}
Throwable cause = e.getCause();
while (cause != null)
{
if (cause instanceof UnknownHostException)
{
//TODO some thing
break;
}
else if (cause instanceof ConnectException)
{
//TODO some thing
break;
}
cause = cause.getCause();
}