javaexceptionunknown-host

UnknownHostException java


My program runs perfectly for sometime time, but after that I get an error

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:666)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.ReviewsSearch.getJsonResponse(ReviewsSearch.java:28)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:110)

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1167)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.productHierarchySearch.getHierarchy(productHierarchySearch.java:27)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:115)

Any idea, what can be the reason? Let me know if more information is required

edit: I tried ping command and I realized it is the internet connection. I am running the program in an ssh server and want to know how can I keep the code from stopping. I want it to wait for the connection.


Solution

  • Other answers address the particular exception you are seeing. However, you (probably) are really asking what to do about it.

    As you have encountered, in the real world things sometimes fail (and throws exceptions). In those cases in which the exception is expected to be temporary, you should be catching the exception and arranging to retry the operation. You may want to retry immediately, but more often it is better to wait a bit before trying again. The idea is to let whatever problem is happening get fixed (or fix itself) before trying again.

    It is often a good idea to log the exception (or at least a summary), in case operator intervention may be required to get things going again.

    If you cannot proceed until that operation is successful, you probably want to retry in a loop. If you can work on other stuff, the retry might be implemented by putting the problem item at the back of the queue of work.

    For the former case:

    doWork("www.sears.com");
    

    becomes

    while (true) {
        try {
            doWork("www.sears.com");
            break;
        } catch (UnknownHostException e) {
            logger.log(e.getMessage());
            Thread.sleep(10000);
        }
    }