【问题标题】:Ignore Error org.jsoup.HttpStatusException... And Print Custom Message?忽略错误 org.jsoup.HttpStatusException... 并打印自定义消息?
【发布时间】:2016-12-03 12:23:20
【问题描述】:

发生了什么

我正在尝试解析 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 忽略此错误,而是在控制台中打印“无效网站”并继续处理?

【问题讨论】:

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


    【解决方案1】:
    try {
    ....
    } catch (HttpStatusException e) {
        System.out.println("Invalid website");
    }
    

    【讨论】:

      【解决方案2】:

      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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-08
        • 2023-03-26
        • 1970-01-01
        • 2019-06-19
        • 1970-01-01
        • 2017-09-26
        相关资源
        最近更新 更多