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

时间:2016-07-28 22:24:27

标签: java eclipse parsing jsoup http-status-code-404

发生了什么

我试图解析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忽略这个错误,而是打印"无效的网站"在控制台中并保持进程?

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

initConnection#69796de9 {X:Type} api_id:int device_model:string system_version:string app_version:string lang_code:string query:!X = X; invokeWithLayer#da9b0d0d {X:Type} layer:int query:!X = X; 不是org.jsoup.Connection.get()

可以抛出的唯一例外
org.jsoup.HttpStatusException

但是,由于所有这些实现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