【问题标题】:"java.io.IOException: Connection timed out" VS HttpTimeoutException On java 11 HTTP Client“java.io.IOException:连接超时”VS HttpTimeoutException 在 java 11 HTTP 客户端
【发布时间】:2020-08-31 07:14:48
【问题描述】:

我正在使用 Java 11 http-client (java.net.http)。

send() 方法声明了这些异常:

@throws IOException
@throws InterruptedException
@throws IllegalArgumentException
@throws SecurityException

我对捕获由超时引起的异常很感兴趣。 我认为最好的方法是抓住 HttpTimeoutException(扩展IOException

但是,我有时会看到发生超时时抛出的异常是:

java.io.IOException: Connection timed out

现在我想知道:

  1. 为什么会抛出更一般的异常?
  2. 应如何编写 catch 以确保捕获所有可能的超时相关异常?

【问题讨论】:

标签: java java-11 timeoutexception java-http-client


【解决方案1】:
@Override
public <T> HttpResponse<T>
send(HttpRequest req, BodyHandler<T> responseHandler)
    throws IOException, InterruptedException
{
    CompletableFuture<HttpResponse<T>> cf = null;
    try {
        cf = sendAsync(req, responseHandler, null, null);
        return cf.get();
    } catch (InterruptedException ie) {
        if (cf != null )
            cf.cancel(true);
        throw ie;
    } catch (ExecutionException e) {
        final Throwable throwable = e.getCause();
        final String msg = throwable.getMessage();

        if (throwable instanceof IllegalArgumentException) {
            throw new IllegalArgumentException(msg, throwable);
        } else if (throwable instanceof SecurityException) {
            throw new SecurityException(msg, throwable);
        } else if (throwable instanceof HttpConnectTimeoutException) {
            HttpConnectTimeoutException hcte = new HttpConnectTimeoutException(msg);
            hcte.initCause(throwable);
            throw hcte;
        } else if (throwable instanceof HttpTimeoutException) {
            throw new HttpTimeoutException(msg);
        } else if (throwable instanceof ConnectException) {
            ConnectException ce = new ConnectException(msg);
            ce.initCause(throwable);
            throw ce;
        } else if (throwable instanceof SSLHandshakeException) {
            // special case for SSLHandshakeException
            SSLHandshakeException he = new SSLHandshakeException(msg);
            he.initCause(throwable);
            throw he;
        } else if (throwable instanceof SSLException) {
            // any other SSLException is wrapped in a plain
            // SSLException
            throw new SSLException(msg, throwable);
        } else if (throwable instanceof IOException) {
            throw new IOException(msg, throwable);
        } else {
            throw new IOException(msg, throwable);
        }
    }
}

请看下面的方法,它是 HttpClientImpl.java 的内部方法。 处理您想要管理的所有异常,并相应地实现您的代码。

如果您处理 HttpConnectTimeoutException、IOException、HttpTimeoutException,那么您将被覆盖。

【讨论】:

  • 您的意思是捕获 IOException 并在消息中搜索是否包含“连接超时”?
  • 那将是糟糕的设计,因为您可以根据将来可能会更改的某些消息来构建异常处理。仅实现 IOException catch 块并使用 instanceOf 运算符进行内部检查并在那里实现您的异常逻辑(据我所知,所有其他异常都在扩展 IOException。这样您只需要实现单个 catch 块)。
  • 同意,但我仍然不明白您打算如何识别超时,当它以“java.io.IOException:连接超时”的形式出现时,捕获 IOException 还不够好因为它被抛出的原因有很多
  • 你是对的,当 IOException 被抛出时,你现在唯一能识别的方法是基于消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多