2016-07-28 84 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(); 
}