【发布时间】:2013-11-06 07:02:03
【问题描述】:
程序无法连接网站时,Jsoup如何进行错误处理?
例如该网站不存在,我想向用户打印错误消息
下面的代码显示了我连接到某个网站的方式,但我想要的是,如果该网站不存在,我希望它打印错误消息。
Document doc;
try {
// need http protocol
doc = Jsoup.connect("https://forum.lowyat.net/user/OldSchoolJoke").get();
// get page title
String title = doc.title();
//System.out.println("title : " + title);
// get all links
Elements links = doc.select("div.postdetails");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("div"));
System.out.println("text : " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】: