当然不能!
public static void main(String[] args) throws Exception
你把异常抛出来,不捕获throws Exception,URL不正确时不报错
你可以这样:
public class URLReader {
public static void main(String[] args) {
try {
URL yahoo = new URL("http://www.sohu.com/");
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) yahoo.openConnection();
System.out.println(l_connection.getResponseMessage());
System.out.println(l_connection.getResponseCode());
}catch(Exception e) {
System.out.println(e.getMessage);
}
}
}
public static void main(String[] args) throws Exception
你把异常抛出来,不捕获throws Exception,URL不正确时不报错
你可以这样:
public class URLReader {
public static void main(String[] args) {
try {
URL yahoo = new URL("http://www.sohu.com/");
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) yahoo.openConnection();
System.out.println(l_connection.getResponseMessage());
System.out.println(l_connection.getResponseCode());
}catch(Exception e) {
System.out.println(e.getMessage);
}
}
}
URL错时,自然会产生异常. 而且你throws出来了 try {
...
}
catch (Exception e) {
System.out.println(e.toString());
}
try{
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) yahoo.openConnection();
}
catch(Exception e){}
System.out.println(l_connection.getResponseMessage());
System.out.println(l_connection.getResponseCode());
}
这样异常也不做处理,继续执行下面的语句,看看打印出来什么信息。