2016-07-28 64 views
1

发生了什么忽略错误org.jsoup.HttpStatusException ...并打印自定义消息?

我试图解析500个不同的链接,检索电子邮件,这个链接是老一些网站已经关闭,以便其正常接收404错误,但它结束的全过程。

PS:下面的代码在循环

代码

  Document doc = Jsoup.connect(link.group()).timeout(20*1000).get(); 
      Matcher m = Pattern.compile("[a-zA-Z0-9_.+-][email protected][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忽略这个错误,而是在控制台中输出“无效的网站”,并让过程继续进行?

回答

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

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