Java 为什么来自Jsoup.connect.execute的响应为空?什么时候抛出IOException?

Java 为什么来自Jsoup.connect.execute的响应为空?什么时候抛出IOException?,java,parsing,jsoup,Java,Parsing,Jsoup,我的代码如下所示: 当我试图用不正确的url调用此方法时,例如执行抛出异常,响应为空。为什么?在这种情况下,我如何获得http代码 public Document getDocumentFromUrl(String url) throws SiteBusinessException { Response response = null; try { response = Jsoup.connect(url).timeout(Constans

我的代码如下所示:

当我试图用不正确的url调用此方法时,例如执行抛出异常,响应为空。为什么?在这种情况下,我如何获得http代码

public Document getDocumentFromUrl(String url) throws SiteBusinessException {
        Response response = null;
        try {
            response = Jsoup.connect(url).timeout(Constans.TIMEOUT).ignoreHttpErrors(false).userAgent(Constans.USER_AGENT)
                    .ignoreContentType(Constans.IGNORE_CONTENT_TYPE).execute();
            return response.parse();
        } catch (IOException ioe) {
            LOGGER.warn("Cannot fetch site ]");
            return null;
        }
    }
编辑

编辑

工作溶液

try {
            response = Jsoup.connect(url).execute();
            return processDocument(response.parse(), url);
        } catch (IllegalArgumentException iae) {
            LOGGER.warn("Malformed URL [url={}, message={}]", new Object[] { url, iae.getMessage() });
            throw new SiteBusinessException(iae.getMessage());
        } catch (MalformedURLException mue) {
            LOGGER.warn("Malformed URL [url={}, message={}]", new Object[] { url, mue.getMessage() });
            throw new SiteBusinessException(mue.getMessage());
        } catch (HttpStatusException hse) {
            LOGGER.warn("Cannot fetch site [url={}, statusMessage={}, statusCode={}]",
                    new Object[] { url, hse.getMessage(), hse.getStatusCode() });
            throw new SiteBusinessException(hse.getMessage(), hse.getStatusCode());
        } catch (IOException ioe) {
            LOGGER.warn("IOException. Cannot fetch site [url={}, errorMessage={}]", url, ioe.getMessage());
            throw new SiteBusinessException("Cannot fetch site");
        }

不,没有。您正在捕获异常并自己返回null。您永远不能在抛出异常的同时返回某些内容


由于主机不存在,因此不会有HTTP代码。HTTP代码由服务器返回。例如,最著名的代码是404(未找到)。当您的浏览器显示404时,它只是服务器发送到客户端的HTTP/TCP数据包,其中包含此代码。

此空值不包含此代码。我在调试模式下查看。
Cannot fetch site [url=http://localhost:8090/wrongaddress/, statusMessage=<null>, statusCode=<null>]
try {
            response = Jsoup.connect(url).execute();
            return processDocument(response.parse(), url);
        } catch (IllegalArgumentException iae) {
            LOGGER.warn("Malformed URL [url={}, message={}]", new Object[] { url, iae.getMessage() });
            throw new SiteBusinessException(iae.getMessage());
        } catch (MalformedURLException mue) {
            LOGGER.warn("Malformed URL [url={}, message={}]", new Object[] { url, mue.getMessage() });
            throw new SiteBusinessException(mue.getMessage());
        } catch (HttpStatusException hse) {
            LOGGER.warn("Cannot fetch site [url={}, statusMessage={}, statusCode={}]",
                    new Object[] { url, hse.getMessage(), hse.getStatusCode() });
            throw new SiteBusinessException(hse.getMessage(), hse.getStatusCode());
        } catch (IOException ioe) {
            LOGGER.warn("IOException. Cannot fetch site [url={}, errorMessage={}]", url, ioe.getMessage());
            throw new SiteBusinessException("Cannot fetch site");
        }