Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JAX-WS Web服务调用中捕获ConnectException_Java_Web Services_Exception Handling_Jax Ws_Webservices Client - Fatal编程技术网

Java 在JAX-WS Web服务调用中捕获ConnectException

Java 在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

我使用JAX-WS2.2.5框架来调用Web服务。我想确定由于Web服务关闭或无法访问而导致调用失败的特殊情况

在一些调用中,我得到一个WebServiceException

    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();
        }