Drei
Drei

Reputation: 19

UnknownHostException using java.net.Socket

I have this class

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));
    }
}

I have this error

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

Upvotes: 0

Views: 855

Answers (1)

Peshal
Peshal

Reputation: 1518

Your log says it all. Try changing

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

To this:

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

Upvotes: 1

Related Questions