小编Kae*_*hen的帖子

Java JSoup 异常忽略 try catch?

我有一个程序,它使用 JSoup 连接到网站并解析网站中的数据。当网站在我指定的 10 秒超时后无法连接时,JSoup 方法会抛出 UncheckedIOException。这可能包含一个 IOException,例如“SSL Peer 意外关闭”或“超时”,这些是我过去处理过的 IOException。这很奇怪,因为它包含在 try catch 中:

        Document document;
        try {
            document = Jsoup.connect("https://www.website.com").timeout(10000).maxBodySize(0).get();
        } catch (Exception e) {
            return false;
        }
        Elements elements = document.select("tr");
        for (Element e : elements) {
            System.out.println(e.text());
        }
        return true;
Run Code Online (Sandbox Code Playgroud)

我已经完成了各种解决方法,例如将方法包装在 try catch 中、使方法抛出异常并在更高层处理它,以及上面我隔离了导致问题的行并仅将其包装在 try catch 中。我尝试过捕获 Exception、IOException、UncheckedIOException 等,但由于错误以某种方式渗透,最终仍然会导致整个应用程序崩溃。

我从来没有遇到过这样的事情。try catch 无法处理错误怎么办?这是 JSoup 设置错误系统的方式吗?这不对吧?我正在使用 JSoup 1.11.2(如果有帮助的话),从https://jsoup.org/download下载。

java exception try-catch jsoup

2
推荐指数
1
解决办法
2212
查看次数

标签 统计

exception ×1

java ×1

jsoup ×1

try-catch ×1