当用户插入一个奇怪的URL时,我的程序崩溃
它应该像
while(condition) {
try {
String url = reciveURL();
Document rss = Jsoup.connect( url ).get();
}
catch (MalformedURLException e) {
System.err.println("Invalid URL");
} catch (OthersExceptions e){
Others.Actions();
}
}
问题是,这将引发“ java.net.MalformedURLException:无协议”,而不是打印“无效的URL”,并且程序崩溃(当用户插入任何其他类型的文本时)
谢谢!
答案 0 :(得分:1)
大声笑,解决了我自己,但我会问这个问题,因为在同一问题上没有任何帖子
您应该导入java.net.url,这会带来“ URL”类型,该类型会触发MalformedURLException(Jsoup不会这样做)
就这样
while(true){
{
String url = reciveURL() ;
URL chk_url = new URL(url);
Document rss = Jsoup.connect( url ).get();
} catch (MalformedURLException e) {
System.err.println("url mal puesta!");
}
}