忽略错误 org.jsoup.HttpStatusException... 并打印自定义消息?

Ignore Error org.jsoup.HttpStatusException... And Print Custom Message?

发生了什么事

我正在尝试解析 500 个不同的链接以从中检索电子邮件,这个链接是旧的并且一些网站已经关闭所以收到 404 错误是正常的,但是它终止了整个过程。

Ps: 下面的代码是运行循环

代码

            Document doc = Jsoup.connect(link.group()).timeout(20*1000).get();
            Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+").matcher(doc.toString());
                if (m.find()) {             
                    String email = m.group();              
                    System.out.println(m.group() + " - " + organizationName.group());


                }
                else {System.out.println("No Emails Found");};

错误

     Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404

我想要的

有什么方法可以告诉 Java/Eclipse 忽略此错误,而是在控制台中打印 "Invalid Website" 并继续进行?

try {
....
} catch (HttpStatusException e) {
    System.out.println("Invalid website");
}

org.jsoup.HttpStatusException 不是 org.jsoup.Connection.get()

可以抛出的唯一异常
MalformedURLException - if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
HttpStatusException - if the response is not OK and HTTP response errors are not ignored
UnsupportedMimeTypeException - if the response mime type is not supported and those errors are not ignored
SocketTimeoutException - if the connection times out
IOException - on error

但是,由于所有这些都实现了 java.io.IOException,您应该在 try/catch 中使用它,而不仅仅是 org.jsoup.HTTPStatusException.

try {
....
} catch (IOException e) { 
     e.printStackTrace();
}