使用java.net.Socket的UnknownHostException

时间:2013-07-24 01:50:45

标签: android exception-handling

我有这个班级

class ClientSocketClass {
    //connect to IP address 74.125.224.72, TCP port 80
    public static java.net.Socket connect1() {
        return(new java.net.Socket("74.125.224.72", 80));
    }

    //connect to www.google.com, TCP port 80
    public static java.net.Socket connect2() {
        return(new java.net.Socket("www.google.com", 80));
    }
}

我有这个错误

error: unreported exception UnknownHostException; must be caught or declared to be thrown
    [javac] return(new java.net.Socket("www.google.com", 80));

1 个答案:

答案 0 :(得分:1)

你的日志说明了一切。尝试更改

    public static java.net.Socket connect2() {
    return(new java.net.Socket("www.google.com", 80));
   }

对此:

    public static java.net.Socket connect2() throws UnknownHostException {
    return(new java.net.Socket("www.google.com", 80));
   }